Solved

pointer to function

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

Suggested Solutions

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…
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

756 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