函数指针和成员成员用法

原文链接: http://www.cnblogs.com/lemonZ/p/3422281.html

《一》一般函数指针用法:


《一》一般函数指针用法:
typedef char (*PTRFUN)(int); 
PTRFUN pFun;
 char glFun(int a){ return;} 
void main()
     pFun = glFun;
     (*pFun)(2);
} 

《二》成员函数指针用法:

template <typename ClassType, typename ParaType>
class CC
{
public:
	/*函数指针类型模板*/
	typedef ParaType (ClassType::*pClassFun)(ParaType, ParaType);

/*函数指针函数模块*/ ParaType Result(ClassType* pClassType, pClassFun fun, ParaType a, ParaType b) { return (pClassType->*fun)(a, b); } };

《三》使用函数指针做代理:

template <class TTpFuncClass>
void ttMakeFuncObject(TTpFuncClass& theFuncObject, void (TTpFuncClass::*pFunction)())
{
(theFuncObject.*pFunction)();
}
class TTFuncObject{
public:
TTFuncObject()
{
}
void add()
{
cout<< "TTFuncObject::add()" << endl;
}
};
int main(int argc, char* argv[])
{
TTFuncObject aTTFuncObject;
ttMakeFuncObject(aTTFuncObject, &TTFuncObject::add);
return 0;
}
posted on 2013-11-13 22:01 无根的泪痕 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lemonZ/p/3422281.html

猜你喜欢

转载自blog.csdn.net/weixin_30455067/article/details/94785571