Advertisement
AI_UBI

Simple callbacks C++ OOP

Jan 7th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <vector>
  4.  
  5. class IDrawAction
  6. {
  7. public:
  8.  
  9.     IDrawAction() {}
  10.     ~IDrawAction() {}
  11.    
  12.     virtual void draw() = 0;
  13. };
  14.  
  15. class Drawer : public IDrawAction
  16. {
  17.     std::vector<IDrawAction *> drawable;
  18.  
  19. public:
  20.  
  21.     Drawer(){}
  22.     ~Drawer() {}
  23.  
  24.     void draw()
  25.     {
  26.         for (const auto &I : drawable)
  27.         {
  28.             I->draw();
  29.         }
  30.     }
  31.  
  32.     void addDrawable(IDrawAction *ptr)
  33.     {
  34.         drawable.push_back(ptr);
  35.     }
  36. };
  37.  
  38. class SomeObject : public IDrawAction
  39. {
  40. public:
  41.     SomeObject() {}
  42.     ~SomeObject() {}
  43.    
  44.     void draw()
  45.     {
  46.         printf("wtf\n");
  47.     }
  48.  
  49. };
  50.  
  51. int main()
  52. {
  53.     Drawer d;
  54.     SomeObject obj;
  55.  
  56.     d.addDrawable(&obj);
  57.     d.addDrawable(&obj);
  58.     d.addDrawable(&obj);
  59.     d.addDrawable(&obj);
  60.  
  61.     d.draw();
  62.  
  63.     getchar();
  64.  
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement