Solved

pointer to function

Posted on 1999-01-26
5
178 Views
Last Modified: 2010-04-02
I have a .h file:

typedef int (*funcptr)();
int f1();
class CSampleClass
{
public:
      funcptr p;
      CSampleClass();
      int f2();
};

In my .cpp file, it is OK for
{
...
CSampleClass a;
a.p=f1;
...
}

But when I have
{
      ...
CSampleClass a;
a.p=a.f2; //I need this.
...
}
it gives me an error:
'=' : cannot convert from 'int (CSampleClass::*)(void)' to 'int (__cdecl *)(void)'

Please help me to solve this problem.
0
Comment
Question by:yingchunli
  • 3
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 30 total points
ID: 1185177
What you need are member pointers, but they aren't too easy to handle. In your case,

typedef int (CSampleClass::*PSAMPLECLASS_F2)();

CSampleClass a
PSAMPECLASS_F2 pMethodF2 = &CSampleClass::f2;

Then, you can use it like this:
(a.*pMethodF2)();

I admit that this is quite cumbersome... ;-)
0
 
LVL 86

Expert Comment

by:jkr
ID: 1185178
Also, note that
typedef int (CSampleClass::*PSAMPLECLASS_F2)();
is applicable for every member to 'CSampleClass' that takes no arguments and returns 'int', so
int CSampleClass::AnotherFunction()
{
//...
}
PSAMPECLASS_F2 pMethodF2 = &CSampleClass::AnotherFunction;

is also legal...

Feel free to ask if you have additional questions on this...



0
 

Author Comment

by:yingchunli
ID: 1185179
Thank you jkr. Your solution works. Here I just wonder why you need the
class instance a:
(a.*pMethodF2)();
How pMethodF2 is related to the class instance.

Why *pMethodF2(); does not work?
0
 
LVL 86

Expert Comment

by:jkr
ID: 1185180
Method pointers work only with an existing instance of a class. This is because
1. they're not real pointers to code, but indices into an object's method table
2. methods are passed an additional, 'inivisible' arguments that represents 'this'. The code of a method is the same one for each object (same location in memory), but the data (i.e. member variables) is stored at different locations.
0
 

Author Comment

by:yingchunli
ID: 1185181
Thank you jkr.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question