Solved

about pointer to point a function.

Posted on 2000-04-03
4
193 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
  • 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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 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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

706 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now