template<typename ReturnType, typename _Class, typename _Func>
ReturnType MyCall(_Class* pObj, _Func pFunc)
{ // 指针问题自己处理
return (pObj->*pFunc)();
}
template<typename ReturnType, typename _Class, typename _Func, typename P1>
ReturnType MyCall(_Class* pObj, _Func pFunc, P1 para1)
{ // 指针问题自己处理
return (pObj->*pFunc)(para1);
}
template<typename ReturnType, typename _Class, typename _Func, typename P1, typename P2>
ReturnType MyCall(_Class* pObj, _Func pFunc, P1 para1, P2 para2)
{ // 指针问题自己处理
return (pObj->*pFunc)(para1, para2);
}
class CTest
{
public:
int Test_0()
{
return 0;
}
int Test_1(int iNum)
{
return iNum;
}
char Test_2(int iNum, int iNum2)
{
return 'A';
}
};
#define MyCallEx(ReturnType, pObj, pFun, ...) MyCall<ReturnType>(pObj, pFun, __VA_ARGS__)
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
CTest* p = NULL;
int iTest_0 = MyCallEx(int, p, &CTest::Test_0);
int iTest_1 = MyCallEx(int, p, &CTest::Test_1, 100);
char iTest_2 = MyCallEx(char, p, &CTest::Test_2, 1, 2);
std::cout << iTest_0 << " " << iTest_1 << " " << iTest_2 << std::endl;
return 0;
}
自己再根据需要封装,可以有各种妙用~~(如果返回类型都一样,可以省略返回值的类型,看起来更像一个普通函数)