模板呼叫多個引數的成員函式
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;
}
自己再根據需要封裝,可以有各種妙用~~(如果返回型別都一樣,可以省略返回值的型別,看起來更像一個普通函式)