Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!

Inheriteance fixed.

By: chunkyguy on Jun 20th, 2013  |  syntax: C++  |  size: 2.07 KB  |  views: 27  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /* OUTPUT
  2. Trying to avoid this: http://pastebin.com/08ySYBrT
  3. Using this solution: http://stackoverflow.com/a/17218370/286094
  4.  
  5. ---- Rendering ImageView ----
  6. View::Renderer
  7. ImageView::Renderer
  8.  
  9. ---- Rendering Sprite ----
  10. View::Renderer
  11. ImageView::Renderer
  12. Sprite::Renderer
  13.  
  14. ---- Rendering AwesomeSprite ----
  15. View::Renderer
  16. ImageView::Renderer
  17. Sprite::Renderer
  18. AwesomeSprite::Renderer
  19. */
  20.  
  21. #include <string>
  22. #include <iostream>
  23.  
  24. using namespace std;
  25.  
  26. class View{
  27. public:
  28.     void Render(){
  29.                 render();
  30.         }
  31. protected:
  32.     class Renderer{
  33.     public:
  34.         Renderer(const View &v){
  35.             cout << "View::Renderer" << endl;
  36.         }
  37.     };
  38. private:
  39.         virtual void render() = 0;
  40. };
  41.  
  42. class ImageView : public View{
  43. protected:
  44.     class Renderer : public View::Renderer{
  45.     public:
  46.         Renderer(const View &v) : View::Renderer(v){
  47.             cout << "ImageView::Renderer" << endl;
  48.         }
  49.     };
  50. private:
  51.     virtual void render(){
  52.         (Renderer(*this));
  53.     }
  54. };
  55.  
  56. class Sprite : public ImageView{
  57. protected:
  58.     class Renderer : public ImageView::Renderer{
  59.     public:
  60.         Renderer(const View &v) : ImageView::Renderer(v){
  61.             cout << "Sprite::Renderer" << endl;
  62.         }
  63.     };
  64. private:
  65.     virtual void render(){
  66.         (Renderer(*this));
  67.     }
  68. };
  69.  
  70. class AwesomeSprite : public Sprite{
  71. protected:
  72.     class Renderer : public Sprite::Renderer{
  73.     public:
  74.         Renderer(const View &v) : Sprite::Renderer(v){
  75.             cout << "AwesomeSprite::Renderer" << endl;
  76.         }
  77.     };
  78. private:
  79.     virtual void render(){
  80.         (Renderer(*this));
  81.     }
  82. };
  83.  
  84. void Draw(View *v){
  85.         v->Render();
  86. }
  87.  
  88. int main(){
  89.         cout << "---- Rendering ImageView ----" << endl;
  90.     ImageView *img = new ImageView;
  91.         Draw(img);
  92.         delete img;
  93.         cout << endl;
  94.  
  95.         cout << "---- Rendering Sprite ----" << endl;
  96.         Sprite *s = new Sprite;
  97.         Draw(s);
  98.         delete s;
  99.         cout << endl;
  100.  
  101.         cout << "---- Rendering AwesomeSprite ----" << endl;
  102.     AwesomeSprite *as = new AwesomeSprite;
  103.         Draw(as);
  104.         delete as;
  105.         cout << endl;
  106.  
  107.     return 0;
  108. }
clone this paste RAW Paste Data