fork download
  1. // 注意 以下內容通通都省略了需要delete的問題
  2.  
  3. #include <iostream>
  4. #include <vector>
  5.  
  6.  
  7. // 這可以不用看
  8. class _Event{
  9. public:
  10. template<typename T, typename TMemFun>
  11. void Attach(T* obj, TMemFun mf) {_handler = new _eventHandler<T, TMemFun>(obj, mf);}
  12. void _RaiseEvent() {_handler->f();}
  13. private:
  14. class _eventHandlerBase;
  15. private:
  16. _eventHandlerBase* _handler;
  17. private:
  18. class _eventHandlerBase{
  19. public:
  20. virtual void f() = 0;
  21. };
  22.  
  23. template<typename T, typename TMemFun>
  24. class _eventHandler : public _eventHandlerBase{
  25. public:
  26. _eventHandler(T* obj, TMemFun mf) : _obj(obj), _mf(mf) {}
  27.  
  28. // overrides
  29. void f() {(_obj->*_mf)();}
  30. private:
  31. T* _obj;
  32. TMemFun _mf;
  33. };
  34. };
  35.  
  36. // 假設出來的Happy
  37. class Happy{
  38. public:
  39. _Event OnSent;
  40. _Event OnReceive;
  41. _Event OnConnect;
  42. };
  43.  
  44. class Handler_Happy{
  45. public:
  46. Handler_Happy(Happy* happy) : _happy(happy) {}
  47.  
  48. // 假設處理函數都是長這樣的
  49. void OnSent() {std::cout << "OnSent: " << _happy << std::endl;}
  50. void OnReceive() {std::cout << "OnReceive: " << _happy << std::endl;}
  51. void OnConnect() {std::cout << "OnConnect: " << _happy << std::endl;}
  52.  
  53. private:
  54. Happy *_happy;
  55. };
  56.  
  57. void test(Happy* happys[], int size_happys, Handler_Happy handlers[])
  58. {
  59. for (int i=0; i<size_happys; i++){
  60. happys[i]->OnSent.Attach(&handlers[i], &Handler_Happy::OnSent);
  61. happys[i]->OnReceive.Attach(&handlers[i], &Handler_Happy::OnReceive);
  62. happys[i]->OnConnect.Attach(&handlers[i], &Handler_Happy::OnConnect);
  63. }
  64. }
  65.  
  66. int main()
  67. {
  68.  
  69. Happy* happys[10];
  70. for (int i=0; i<10; i++)
  71. happys[i] = new Happy();
  72.  
  73. std::vector<Handler_Happy> handlers;
  74. for (int i=0; i<10; i++){
  75. handlers.push_back(Handler_Happy(happys[i]));
  76. }
  77.  
  78. test(happys, 10, &handlers[0]); // 假設10個
  79.  
  80. for (int i=0; i<10; i++)
  81. happys[i]->OnSent._RaiseEvent();
  82.  
  83. for (int i=0; i<10; i++)
  84. happys[i]->OnReceive._RaiseEvent();
  85.  
  86. for (int i=0; i<10; i++)
  87. happys[i]->OnConnect._RaiseEvent();
  88.  
  89. return 0;
  90. }
  91.  
Success #stdin #stdout 0.02s 2860KB
stdin
Standard input is empty
stdout
OnSent: 0x937a008
OnSent: 0x937a018
OnSent: 0x937a028
OnSent: 0x937a038
OnSent: 0x937a048
OnSent: 0x937a058
OnSent: 0x937a068
OnSent: 0x937a078
OnSent: 0x937a088
OnSent: 0x937a098
OnReceive: 0x937a008
OnReceive: 0x937a018
OnReceive: 0x937a028
OnReceive: 0x937a038
OnReceive: 0x937a048
OnReceive: 0x937a058
OnReceive: 0x937a068
OnReceive: 0x937a078
OnReceive: 0x937a088
OnReceive: 0x937a098
OnConnect: 0x937a008
OnConnect: 0x937a018
OnConnect: 0x937a028
OnConnect: 0x937a038
OnConnect: 0x937a048
OnConnect: 0x937a058
OnConnect: 0x937a068
OnConnect: 0x937a078
OnConnect: 0x937a088
OnConnect: 0x937a098