Advertisement
Redee

шаблонный CALLBACK "костыль" - под любые классы с методом D

Jun 6th, 2016
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. /////////
  5. // шаблонный CALLBACK "костыль"
  6. // под любые классы с методом Display()
  7. ////
  8.  
  9. template< typename T >
  10. class do_Display
  11. {
  12.     public:
  13.         static void start(void* ptObj)
  14.         {
  15.             T* myDisp = (T*) ptObj;
  16.             cout << "callBk >> ";
  17.             myDisp->Display();
  18.         }
  19. };
  20.  
  21. void do_CallBk_with_Obj(void (*pt2Function)(void* pt2Object), void* pt2Object)
  22. {
  23.     pt2Function(pt2Object);
  24. }
  25.  
  26. struct IDisplay
  27. {
  28.     virtual void Display() = 0;
  29. };
  30.  
  31. class Display_A : IDisplay
  32. {
  33.     void Display(){ cout << "Display_A" << endl; }
  34. };
  35.  
  36. class Display_B : IDisplay
  37. {
  38.     void Display(){ cout << "Display_B" << endl; }
  39. };
  40.  
  41. struct Display_C
  42. {
  43.     void Display(){ cout << "Display_C" << endl; }
  44. };
  45.  
  46.  
  47. void main()
  48. {
  49.     Display_A dA;
  50.     do_CallBk_with_Obj( do_Display<IDisplay>::start, &dA );
  51.  
  52.     Display_B dB;
  53.     do_CallBk_with_Obj( do_Display<IDisplay>::start, &dB );
  54.  
  55.     Display_C dC;
  56.     do_CallBk_with_Obj( do_Display<Display_C>::start, &dC );
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement