pointer to member function
I want to use in my class a pointer to function.
I could not find a way.
This is a simple example that explains the difficulty:
Code:
class C{
public:
int First (int i, int j){return i;}
int Second(int i, int j){return j;}
int Fun(int);
};
int C::Fun(int i){
int (C::*pFun)(int, int);
if(i==1)
pFun=&C::First;
else
pFun=&C::Second;
return pFun(1,2); //ERROR
}
this causes an error message to be displayed.
I cannot find a way to solve this. On the other hand the non-member version works correctly, as follows:
Code:
int First(int i, int j);
int Second(int i, int j);
int First(int i, int j){ return i;}
int Second(int i, int j){ return j;}
int main (int argc, const char * argv[])
{
int i=1;
float x;
int (*fun)(int, int);
if(i==1)
fun=First;
else
fun=Second;
x=fun(1,2); //OK
}
Does anyone have any suggestion to give?
Re: pointer to member function
I assume your requirement is more complicated than your example because you don't need function pointers to solve it as written.
You can have a simple function pointer to a free function or a static member function. Simple function pointers cannot reference a member function because they lack the "this" pointer to the instance of the object.
There are several approaches to this. One is in the TR1 extensions to the C++ library:
Code:
#include <tr1/functional>
int C::Fun(int i){
std::tr1::function<int(int,int)> pFun;
if(i==1)
pFun = std::tr1::bind(&C::First,
this,
std::tr1::placeholders::_1,
std::tr1::placeholders::_2);
else
pFun = std::tr1::bind(&C::Second,
this,
std::tr1::placeholders::_1,
std::tr1::placeholders::_2);
return pFun(1,2);
}
Re: pointer to member function
to use pointers to member functions you also need to have an instance!
Code:
class C{
public:
int First (int i, int j){return i;}
int Second(int i, int j){return j;}
int Fun(int);
};
int C::Fun(int i){
int (C::*pFun)(int, int);
if(i==1)
pFun=&C::First;
else
pFun=&C::Second;
return this->*pFun(1,2); //<<<<<<<<<<<<<
}
untested...
Re: pointer to member function
ChrisW67, thank you very much for your complete explanation of the issue.
I can accept to convert the functions to static and everything works.
BTW, just in case you are curious, my mechanism of function pointer is inside a routine that calculates "round" numbers for the extrema of plots.
For instance a plot that ranges between 2.345 and 32.84 could not have on the axes those nasty float numbers. Better to choose, in this case, to plot between 0 and 40. To understand what extrema to choose in all possible situations is a demanding task and I decided to take advantage to a choice of functions to select from at run time.
To reduce the number of if's in my code I prefer to point to the right function and use later the pointer instead of the native function.
Amleto, your suggestion did not work. I think the reason its the one explained by ChrisW67.
Thanks again to both for helping me.
Re: pointer to member function
no its just a syntax mistake.
this works:
return (this->*pFun)(1,2);