Solved

about pointer to point a function.

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C Language combined operators 28 109
What is the real difference between C++ and Visual C++.NET? 7 114
FMX enumerated colours 2 99
is twain_32.dll cmpatible with windows 10 ? 10 162
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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…

770 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