Callback_05.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include <iostream>
  2. #include <functional>
  3. using std::cout;
  4. using std::endl;
  5. class EventHandler
  6. {
  7. public:
  8. /*
  9. template<typename T>
  10. void addHandler(T* owner)
  11. {
  12. cout << "Handler added..." << endl;
  13. //Let's pretend an event just occured
  14. owner->Callback(owner, 1);
  15. }
  16. */
  17. void addHandler(std::function<void(int)> callback)
  18. {
  19. cout << "Handler added..." << endl;
  20. // Let's pretend an event just occured
  21. callback(1);
  22. }
  23. };
  24. EventHandler* handler;
  25. /**********************************************************************************************
  26. *
  27. */
  28. class MyClass
  29. {
  30. public:
  31. MyClass() {
  32. using namespace std::placeholders;
  33. private_x = 5;
  34. // handler->addHandler(this);
  35. handler->addHandler(std::bind(&MyClass::Callback, this, _1));
  36. }
  37. void Callback(int x) {
  38. cout << x + private_x << endl;
  39. }
  40. int public_x = 12;
  41. private:
  42. int private_x;
  43. };
  44. /**********************************************************************************************
  45. *
  46. */
  47. class YourClass
  48. {
  49. public:
  50. YourClass()
  51. {
  52. using namespace std::placeholders;
  53. private_y = 8;
  54. handler->addHandler(std::bind(&YourClass::Callback, this, _1));
  55. }
  56. void Callback(int x)
  57. {
  58. cout << x + private_y << endl;
  59. }
  60. private:
  61. int private_y;
  62. };
  63. /**********************************************************************************************
  64. *
  65. */
  66. void freeStandingCallback(int x)
  67. {
  68. cout << x << endl;
  69. }
  70. /**********************************************************************************************
  71. *
  72. */
  73. // https://en.cppreference.com/w/cpp/language/function_template
  74. template<class X>
  75. void f(X a) {
  76. printf("Test");
  77. }
  78. template<class X>
  79. void f(X* a) {
  80. printf("Test");
  81. }
  82. void (*p)(int*) = &f;
  83. // https://en.cppreference.com/w/cpp/language/function_template
  84. // template<class T1, class T2>
  85. void g(MyClass* a1, YourClass* a2) {
  86. printf("access both classes\n");
  87. printf("MyClass: %d\n", a1->public_x);
  88. printf("YourClass: private\n");
  89. }
  90. int main(int argc, char** argv)
  91. {
  92. handler = new EventHandler();
  93. MyClass* myClass = new MyClass();
  94. YourClass* yourClass = new YourClass();
  95. handler->addHandler([](int x) {
  96. std::cout << "x is " << x << '\n';
  97. });
  98. handler->addHandler(freeStandingCallback);
  99. f(myClass->public_x);
  100. g(myClass, yourClass);
  101. }