//共有基类
class IConsign
{
public:
IConsign(){}
virtual ~IConsign(){}
};
//保存类指针和函数指针的类
#define INVOKE(Delegate) (Delegate.This->*Delegate.Handle)
template<typename classtype,typename funtype>
class CDelegate
{
public:
CDelegate()
{
This=0;
Handle=0;
}
CDelegate(classtype *p,funtype f)
{
This=p;
Handle=f;
}
virtual ~CDelegate()
{
This=0;
Handle=0;
}
public:
void Bind(classtype *p,funtype f)
{
This=p;
Handle=f;
}
public:
classtype *This; //类指针
funtype Handle; //函数指针
};
//使用方式
//定义一些函数指针
typedef void (IConsign::*ControlEvent)();
typedef void (IConsign::*ControlEventEx)(IConsign* sender);
typedef CDelegate<IConsign,ControlEvent> ControlDelegate;
typedef CDelegate<IConsign,ControlEventEx> ControlDelegateEx;
//回调
ControlDelegate cd;
cd.Bind(...);
INVOKE(cd)
观察者模式
最新推荐文章于 2011-12-07 20:05:00 发布