比如:int (*foo)(int arg),记住要和另一个指针函数区分开来,类似这样:int *foo(int arg).
比如我们可以这样声明一个变量和函数:
int (*pfun)(int arg)=0;
int fun(int arg); //这个函数实现随便啦,我就不写了。
如果我们想利用函数指针操作函数,就和指针变量使用一样:
pfun=fun;
int result=(*pfun)(123);
对,很鸡肋也没必要。这是当然,因为我们没用在对的地方。下面我要讲的是利用一个类去call back另一个无关类的成员。
代码:
#include
using namespace std;
template
class Functor{
public:
Functor(T *otherp,N (T::*otherfun)(N arg))
{
mp=otherp;
mfun=otherfun;
}
virtual N operator()(N arg)
{
return (*mp.*mfun)(arg);
}
private:
N (T::*mfun)(N arg);
T *mp;
};
class A{
public:
A(int a0):a(a0){}
int traced(int b)
{ <