回调函数c++实现。

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

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


第二版
[cpp]  view plain copy
  1. /* 
  2. 类的成员变量要封装成私有的,对数据的保护 
  3. 在类外面访问类的私有成员可以通过类的成员函数来访问 
  4. */  
  5. //def.h  
  6. #include <iostream>  
  7. #include <stdio.h>  
  8. using namespace std;  
  9.   
  10. typedef enum  
  11. {  
  12.     CB_MOVE = 0,    //    
  13.     CB_COMEBACK,    //  
  14.     CB_BUYEQUIIP,   //  
  15. }cb_type;  
  16.   
  17. typedef void(*cb_func)(void *);  
  18.   
  19. class CCommu //模块类  
  20. {  
  21. public:  
  22.     CCommu()  
  23.     {  
  24.         memset(func_list, 0, sizeof(cb_func) *(CB_BUYEQUIIP +1));  
  25.         memset(func_args, 0, sizeof(void *) *(CB_BUYEQUIIP +1));  
  26.         cout<<"sth in CCommu"<<endl;  
  27.     }  
  28.   
  29.     int reg_cb(cb_type type, cb_func func, void *args = NULL)//注册回调函数   
  30.     {  
  31.         if(type <= CB_BUYEQUIIP)   
  32.         {  
  33.             func_list[ type ] = func;  
  34.             func_args[type] = args;  
  35.             return 0;  
  36.         }  
  37.     }  
  38. public:  
  39.     cb_func func_list[CB_BUYEQUIIP + 1] ;   //函数指针数组  
  40.     void * func_args[CB_BUYEQUIIP +1];  
  41. };  
[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.     //获取CCommu *  
  20.     CCommu * GetCommuPoint();  
  21.   
  22. private:  
  23.     CCommu *m_pCommu;   //对数据的保护  
  24.   
  25. };  
[cpp]  view plain copy
  1. //Gamestart.cpp  
  2. #include "Gamestart.h"  
  3.   
  4. CGameStart::CGameStart()  
  5. {  
  6.     CCommu *pCommu = new CCommu;//pCommu是栈变量。在这里分配的内存必须赋给类的成员变量,  
  7.                                 //不然局部出了函数释放了,就找不到这块内存,造成内存泄露  
  8.     m_pCommu = pCommu;  
  9. }  
  10.   
  11. void CGameStart::Init() //初始化的时候,注册回调函数   
  12. {  
  13.     m_pCommu ->reg_cb(CB_MOVE, Move , this);  
  14.     m_pCommu->reg_cb (CB_COMEBACK, Comeback,this );  
  15. }  
  16.   
  17. void CGameStart::run()  
  18. {  
  19.     Init();  
  20. }  
  21.   
  22. void CGameStart::Execute()  
  23. {  
  24.     cout<<"callback funciton is running"<<endl;  
  25.   
  26. }  
  27. CGameStart::~CGameStart()  
  28. {  
  29.     if(m_pCommu != NULL)  
  30.     {  
  31.         delete m_pCommu;  
  32.         m_pCommu = NULL;  
  33.     }  
  34. }  
  35.   
  36. void CGameStart::Move(void *args)  
  37. {  
  38.     CGameStart *pGame  = (CGameStart *)args;  
  39.     pGame -> Execute();  
  40. }  
  41. void CGameStart::Comeback(void *args)   
  42. {  
  43.     //char *str = (char *)args;  
  44.     //cout << str <<endl;  
  45. }  
  46.   
  47. CCommu * CGameStart::GetCommuPoint()  
  48. {  
  49.         return m_pCommu;  
  50. }  
[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->GetCommuPoint()->func_list[CB_MOVE] != NULL)//回调函数的触发  
  10.     {  
  11.         pGame->GetCommuPoint()->func_list[CB_MOVE](pGame->GetCommuPoint()->func_args[CB_MOVE]);  
  12.     }  
  13.     return 0;  
  14. }  

你可能感兴趣的:(回调函数c++实现。)