回调函数c++实现

Callback最本质的特征包括两点:注册和触发

C++中如何将类成员函数作为回调函数使用,必须是静态方法吗?

必须是静态成员函数或者全局函数来实现回调函数
大概原因是普通的C++成员函数都隐含了一个传递函数作为参数,即this指针,C++通过传递this指针给成员函数从而实现函数可以访问C++的数据成员。由于this指针的原因,使得一个普通成员函数作为回调函数时就会因为隐含的this指针问题使得函数参数个数不匹配,从而导致回调函数编译失败。


[cpp]  view plain  copy
  1. //def.h  
  2. #include <iostream>  
  3. #include <stdio.h>  
  4. using namespace std;  
  5.   
  6. typedef enum  
  7. {  
  8.     CB_MOVE = 0,    //    
  9.     CB_COMEBACK,    //  
  10.     CB_BUYEQUIIP,   //  
  11. }cb_type;  
  12.   
  13. typedef void(*cb_func)(void *);  
  14.   
  15. class CCommu //模块类  
  16. {  
  17. public:  
  18.     CCommu()  
  19.     {  
  20.         memset(func_list, 0, sizeof(cb_func) *(CB_BUYEQUIIP +1));  
  21.         memset(func_args, 0, sizeof(void *) *(CB_BUYEQUIIP +1));  
  22.     }  
  23.   
  24.     int reg_cb(cb_type type, cb_func func, void *args = NULL)//注册回调函数   
  25.     {  
  26.         if(type <= CB_BUYEQUIIP)   
  27.         {  
  28.             func_list[ type ] = func;  
  29.             func_args[type] = args;  
  30.             return 0;  
  31.         }  
  32.     }  
  33. public:  
  34.     cb_func func_list[CB_BUYEQUIIP + 1] ;   //函数指针数组  
  35.     void * func_args[CB_BUYEQUIIP +1];  
  36. };  

[cpp]  view plain  copy
  1. //Gamestart.h  
  2. #include "def.h"  
  3.   
  4. class CGameStart  
  5. {  
  6.   
  7. public:  
  8.     CGameStart();  
  9.     ~CGameStart();  
  10.     void Init();  
  11.     void run();  
  12.     void Execute();  
  13.       
  14.     //一些回调函数  
  15.     void static Move(void *args);  
  16.     void static Comeback(void *args);  
  17.     void static Buyequip(void *args);  
  18.   
  19. public:  
  20.     CCommu *pCommu;  
  21.   
  22. };  

[cpp]  view plain  copy
  1. //Gamestart.cpp  
  2. #include "Gamestart.h"  
  3.   
  4. CGameStart::CGameStart():pCommu(NULL)  
  5. {}  
  6.   
  7. void CGameStart::Init() //初始化的时候,注册回调函数   
  8. {  
  9.     pCommu  = new CCommu;  
  10.     pCommu ->reg_cb(CB_MOVE, Move , this);  
  11.     pCommu->reg_cb (CB_COMEBACK, Comeback,this );  
  12. }  
  13.   
  14. void CGameStart::run()  
  15. {  
  16.     Init();  
  17. }  
  18.   
  19. void CGameStart::Execute()  
  20. {  
  21.     cout<<"callback funciton is running"<<endl;  
  22.   
  23. }  
  24. CGameStart::~CGameStart()  
  25. {  
  26.     if(pCommu != NULL)  
  27.     {  
  28.         delete pCommu;  
  29.         pCommu = NULL;  
  30.     }  
  31. }  
  32.   
  33. void CGameStart::Move(void *args)  
  34. {  
  35.     CGameStart *pGame  = (CGameStart *)args;  
  36.     pGame -> Execute();  
  37. }  
  38. void CGameStart::Comeback(void *args)   
  39. {  
  40.     //char *str = (char *)args;  
  41.     //cout << str <<endl;  
  42. }  
[cpp]  view plain  copy
  1. //main.cpp  
  2. #include "Gamestart.h"  
  3.   
  4. int main()  
  5. {  
  6.   
  7.     CGameStart *pGame = new CGameStart;  
  8.     pGame -> run();  
  9.     if(pGame->pCommu->func_list[CB_MOVE] != NULL)//回调函数的触发  
  10.     {  
  11.         pGame->pCommu->func_list[CB_MOVE](pGame->pCommu->func_args[CB_MOVE]);  
  12.     }  
  13.     return 0;  
  14. }  

转载地址:

http://blog.csdn.net/gxut555/article/details/7534359

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值