Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. struct Invoker;
  7. class X;
  8.  
  9. typedef void (*printerType)(X*, int);
  10.  
  11. class X
  12. {
  13. private:
  14.     int b;
  15.     static void printsum(X* instance, int a)
  16.     {
  17.         cout << a + instance->b << endl;
  18.     }
  19. public:
  20.     X* setb(int b)
  21.     {
  22.         this->b = b;
  23.         return this;
  24.     }
  25.  
  26.     Invoker* getPrinter()
  27.     {
  28.         Invoker i;
  29.         i.instance = this;
  30.         i.func = (printerType)printsum;
  31.         return new Invoker(i);
  32.     }
  33. };
  34.  
  35. struct Invoker
  36. {
  37.     X* instance;
  38.     void (*func)(X*, int);
  39.  
  40.     void Invoke(int a)
  41.     {
  42.         func(instance, a);
  43.     }
  44. };
  45.  
  46. int main()
  47. {
  48.     X* obj = new X;
  49.     obj->setb(5)->getPrinter()->Invoke(5);
  50.  
  51.     system("pause");
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement