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;
}

原文链接: https://www.cnblogs.com/shit/p/3229827.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/98107

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月10日 上午4:33
下一篇 2023年2月10日 上午4:36

相关推荐