Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# about pointer to point a function.

Posted on 2000-04-03
Medium Priority
244 Views
In the class A has a pointer P1 which has been defined point to function. The class A have function F1,F2,F3 too.In the F3 I write P1=F1; But it is wrong! when I want to build it.
class A
{
public:
int F1(int i);
int F2(int i);
int F3(int i);
int (*P1)(int i);
}

int F3(int i)
{
P1=F1;   //here will be wrong!
*P1(7);
}

pleas give me answer with exampl.
thank you.
:-)
0
Question by:kanyuchun
[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
• 2
• 2

LVL 9

Accepted Solution

jasonclarke earned 600 total points
ID: 2680730
The problem is that you are trying to get a pointer to a *member* function.  Pointers to members are not the same as ordinary pointers to functions.

Here is your code modified to use pointers to members.

class A
{
public:
int F1(int i);
int F2(int i);
int F3(int i);
int (A::*P1)(int i);
};

int F3(int i)
{
A a;
a.P1= A::F1;

(a.*a.P1)(7);
}

0

Author Comment

ID: 2682221
Thanks jasonclarke very much!
Are there any other way to do it ?
0

LVL 9

Expert Comment

ID: 2682967
Depends what exactly you are trying to do.  You can encapsulate the behaviour of a member function pointer in a class, or you can call static member functions in the normal way.

The basic problem is thought that to call a method you need both an object instance and a method pointer.  Let me know more about what you want, and maybe I can help.
0

Author Comment

ID: 2683065
hi,jasonclarke
thank you very much!
I have solved my problem already!
~(@_@)~
0

## Featured Post

Question has a verified solution.

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

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
###### Suggested Courses
Course of the Month8 days, 18 hours left to enroll