c++ 宏定义声明类,并在类中实现回调

#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
typedef void ( *tFunc )( void );
class subclass{
public:
    virtual tFunc setFun() = 0;
};
#define __SetCallBack1(op_name,wrapper_name) \
class op_name:public subclass{ \
public:\
op_name(tFunc func):mfunc(func){}\
tFunc setFun(){return mfunc;}\
tFunc mfunc;\
};  \
inline op_name  wrapper_name(tFunc func){return op_name(func);}

#define _SetCallBack1(name) __SetCallBack1(name##T,name)
_SetCallBack1(SetInitFunc)

template<class OP1>
void InitControl( OP1 op1)
{
    subclass *msc=&op1;
    tFunc f = msc->setFun();
    f();
}
void  printss()
{
    cout<<"hello"<<endl;
}
int main()
{
    
    InitControl(SetInitFunc(printss));
    cin.get();
    return 0;
}

 

posted @ 2013-08-01 13:45  newShit  阅读(2687)  评论(0编辑  收藏