CallbackTest_01.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* printf example */
  2. #include <stdio.h>
  3. #if 0
  4. class MyClass {
  5. public:
  6. // This is our application callback handler for when a message is received, we will
  7. // pass this into the library which deals with message parsing
  8. void onMsg(int num1, int num2) {
  9. printf("onMsg() called with num1=%i, num2=%i\n", num1, num2);
  10. }
  11. };
  12. class LibraryClass {
  13. public:
  14. // For the library class to call the onMsg, it has to be passed both an instance
  15. // of MyClass and a pointer to the member function to call
  16. // Note that MyClass has to be known here! This creates undesired coupling...in
  17. // reality your library should never have to know about MyClass
  18. void passACallbackToMe(void (MyClass::* onMsg)(int num1, int num2)) {
  19. // Call the callback function
  20. (onMsg)(1, 2);
  21. }
  22. };
  23. void onMsg(int num1, int num2) {
  24. printf("onMsg() called with num1=%i, num2=%i\n", num1, num2);
  25. }
  26. int main() {
  27. MyClass myClass;
  28. LibraryClass libraryClass;
  29. // Provide the instance and function to call
  30. libraryClass.passACallbackToMe(&myClass, &MyClass::onMsg);
  31. libraryClass.passACallbackToMe((onMsg)(1,2));
  32. }
  33. #else
  34. #include <cstdio>
  35. void globalfunction(int i) { printf("Global (%i)\n", i); }
  36. static void staticglobalfunction(int i) {
  37. printf("Static Global (%i)\n", i);
  38. }
  39. namespace Name_Space {
  40. void namespacefunc(int i) {
  41. printf("Namespace (%i)\n", i);
  42. }
  43. }
  44. void (*returnfunction(const char* str))(int) {
  45. printf("Return \"%s\":\n", str);
  46. return &globalfunction;
  47. }
  48. typedef void (*functiontype)(int);
  49. class Test {
  50. public:
  51. void (*funcptr1)(int i);
  52. void (Test::* funcptr2)(int i);
  53. void classfunction(int i) {
  54. printf("Class (%i)\n", i);
  55. }
  56. static void staticclassfunction(int i) {
  57. printf("Static Class (%i)\n", i);
  58. }
  59. void callglobal(int i) {
  60. funcptr1 = &globalfunction;
  61. (*funcptr1)(i);
  62. }
  63. void callclass(int i) {
  64. funcptr2 = &Test::classfunction;
  65. (this->*funcptr2)(i);
  66. }
  67. };
  68. int main() {
  69. Test test;
  70. // Global function with local function-pointer
  71. void (*funcptr1)(int i) = &globalfunction;
  72. (*funcptr1)(1);
  73. // Static global function with local function-pointer
  74. void (*funcptr2)(int i) = &staticglobalfunction;
  75. (*funcptr2)(2);
  76. // Function in namespace with local function pointer
  77. void (*funcptr3)(int i) = &Name_Space::namespacefunc;
  78. (*funcptr3)(3);
  79. // Static member function with local function pointer
  80. void (*funcptr4)(int i) = &Test::staticclassfunction;
  81. (*funcptr4)(4);
  82. // Global function with class-function-ptr inside class
  83. test.callglobal(5);
  84. // Global function with class-function-ptr outside class
  85. test.funcptr1 = &globalfunction;
  86. (*test.funcptr1)(6);
  87. // Class function with class-function-ptr inside class
  88. test.callclass(7);
  89. // Class function with local function-ptr
  90. void (Test:: * classfunc)(int i) = &Test::classfunction;
  91. (test.*classfunc)(8);
  92. // Class function with class-function-ptr outside class
  93. test.funcptr2 = &Test::classfunction;
  94. (test.*(test.funcptr2))(9);
  95. (*returnfunction("global"))(10);
  96. printf("Using typedef:\n");
  97. functiontype typefuncptr = &globalfunction;
  98. (*typefuncptr)(11);
  99. return 0;
  100. }
  101. #endif