Solved

pointer to function

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

Industry Leaders: 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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

726 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