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

时间:2023-03-09 23:43:07
c++   宏定义声明类,并在类中实现回调
#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
typedef void ( *tFunc )( void );
class subclass{
public:
virtual tFunc setFun() = ;
};
#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 ;
}