Solved

about pointer to point a function.

Posted on 2000-04-03
4
227 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 200 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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

733 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