声明
class CMyClass; //要先声明类
typedef BOOL (CMyClass::*TestFun)(const CString&, CString&); //定义函数指针TestFun类型
class CMyClass
{
public:
CMyClass(){}
~CMyClass(){}
public:
BOOL Test1(const CString& str1, CString& str2)
{
return false;
}
BOOL Test2(const CString& str1, CString& str2);
{
return false;
}
BOOL TestAction(TestFun mytest);
};
函数指针赋值
TestAction(&CMyClass::Test1);
TestAction(&CMyClass::Test2);
函数指针调用
BOOL CMyClass::TestAction(TestFun mytest)
{
CString str1;
CString str2;
return (this->*mytest)(str1, str2); //类成员函数指针的使用
}
总结
class CMyClass; //要先声明类
typedef BOOL (CMyClass::*TestFun)(const CString&, CString&); //定义函数指针TestFun类型
TestFun mytestFun; //定义指针
mytestFun= &CMyClass::Test1; //指针赋值
bool res = (this->*mytestFun)(str1, str2); //指针调用
mytestFun= &CMyClass::Test2; //指针赋值
bool res = (this->*mytestFun)(str1, str2); //指针调用