Solved

pointer to function

Posted on 1999-01-26
5
154 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now