64,662
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
class information {
public:
typedef void(*PFuncInt)(int, int);
PFuncInt myPfunc; // 定义成员函数指针
void setPFunc(PFuncInt myPf)
{
myPfunc = myPf;
cout << typeid(myPf).name() << endl;
}
void CallPFunc() { myPfunc(3, 4); }
};
void test(int size, int buf)
{
cout << "MyTest (" << size << ", " << buf << ")"<< endl;
}
int main()
{
information f1;
f1.setPFunc(test); //设置成员函数指针
f1.CallPFunc(); //调用
system("PAUSE");
return 0;
}
template<typename T>
class MemberFunctionSlot : public SlotFunctorBase
{
public:
//! Member function slot type.
typedef bool(T::*MemberFunctionType)(const EventArgs&);
MemberFunctionSlot(MemberFunctionType func, T* obj) :
d_function(func),
d_object(obj)
{}
virtual bool operator()(const EventArgs& args)
{
return (d_object->*d_function)(args);
}
private:
MemberFunctionType d_function;
T* d_object;
};