Callback最本质的特征包括两点:注册和触发
C++中如何将类成员函数作为回调函数使用,必须是静态方法吗?
必须是静态成员函数或者全局函数来实现回调函数
大概原因是普通的C++成员函数都隐含了一个传递函数作为参数,即this指针,C++通过传递this指针给成员函数从而实现函数可以访问C++的数据成员。由于this指针的原因,使得一个普通成员函数作为回调函数时就会因为隐含的this指针问题使得函数参数个数不匹配,从而导致回调函数编译失败。
-
- #include <iostream>
- #include <stdio.h>
- using namespace std;
-
- typedef enum
- {
- CB_MOVE = 0,
- CB_COMEBACK,
- CB_BUYEQUIIP,
- }cb_type;
-
- typedef void(*cb_func)(void *);
-
- class CCommu
- {
- public:
- CCommu()
- {
- memset(func_list, 0, sizeof(cb_func) *(CB_BUYEQUIIP +1));
- memset(func_args, 0, sizeof(void *) *(CB_BUYEQUIIP +1));
- }
-
- int reg_cb(cb_type type, cb_func func, void *args = NULL)
- {
- if(type <= CB_BUYEQUIIP)
- {
- func_list[ type ] = func;
- func_args[type] = args;
- return 0;
- }
- }
- public:
- cb_func func_list[CB_BUYEQUIIP + 1] ;
- void * func_args[CB_BUYEQUIIP +1];
- };
-
- #include "def.h"
-
- class CGameStart
- {
-
- public:
- CGameStart();
- ~CGameStart();
- void Init();
- void run();
- void Execute();
-
-
- void static Move(void *args);
- void static Comeback(void *args);
- void static Buyequip(void *args);
-
- public:
- CCommu *pCommu;
-
- };
-
- #include "Gamestart.h"
-
- CGameStart::CGameStart():pCommu(NULL)
- {}
-
- void CGameStart::Init()
- {
- pCommu = new CCommu;
- pCommu ->reg_cb(CB_MOVE, Move , this);
- pCommu->reg_cb (CB_COMEBACK, Comeback,this );
- }
-
- void CGameStart::run()
- {
- Init();
- }
-
- void CGameStart::Execute()
- {
- cout<<"callback funciton is running"<<endl;
-
- }
- CGameStart::~CGameStart()
- {
- if(pCommu != NULL)
- {
- delete pCommu;
- pCommu = NULL;
- }
- }
-
- void CGameStart::Move(void *args)
- {
- CGameStart *pGame = (CGameStart *)args;
- pGame -> Execute();
- }
- void CGameStart::Comeback(void *args)
- {
-
-
- }
-
- #include "Gamestart.h"
-
- int main()
- {
-
- CGameStart *pGame = new CGameStart;
- pGame -> run();
- if(pGame->pCommu->func_list[CB_MOVE] != NULL)
- {
- pGame->pCommu->func_list[CB_MOVE](pGame->pCommu->func_args[CB_MOVE]);
- }
- return 0;
- }