?
Solved

about pointer to point a function.

Posted on 2000-04-03
4
Medium Priority
?
241 Views
Last Modified: 2010-04-02
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
Comment
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
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
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

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

Expert Comment

by:jasonclarke
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

by:kanyuchun
ID: 2683065
hi,jasonclarke
thank you very much!
I have solved my problem already!
~(@_@)~
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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

764 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