Solved

pointer to function

Posted on 1999-01-26
5
189 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
convert char array to number in c 5 93
best sources to up-to-date in C++? 8 115
C++ error cannot convert from std::string to const char * 6 367
Need some help with design 17 46
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

732 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