title: Designing a class hierarchy for game objects in C++
body: <p>I am looking for some advice on designing a class hierarchy for game objects. For this discussion I will mainly talk about two specific classes. An <code>Object</code> and <code>Sprite</code>.</p>
  
  <p>The <code>Object</code> is the base class for any type of object in the game. <code>Sprite</code> inherits directly from <code>Object</code>, but some of <code>Object</code>'s public methods no longer make sense. Here is some code to explain:</p>
  
  <pre><code>class Object {
  public:
      Object() : renderer(NULL), controller(NULL), collider(NULL) { }
      virtual ~Object();
  
      Renderer *getRenderer() const;
      Controller *getController() const;
      Collider *getCollider() const;
  
      void setRenderer(Renderer *renderer);
      void setController(Controller *controller);
      void setCollider(Collider *collider);
  private:
      Renderer *renderer;
      Controller *controller;
      Collider *collider;
  };
  </code></pre>
  
  <p><code>Object</code> has three components a renderer, collider, and a controller.  <code>Sprite</code> of course has these three components, but uses a <code>SpriteRenderer</code>. I figured then I would not want the <code>setRenderer</code> method exposed for the sprite class.</p>
  
  <p>So I thought that I should write the sprite class as follows:</p>
  
  <pre><code>class Sprite : protected Object {
  public:
      Sprite(Texture *texture) : Object(), texture(texture) {
          setRenderer(new SpriteRenderer(this));
      }
  
      Renderer *getRenderer() const;
      Controller *getController() const;
      Collider *getCollider() const;
  
      void setController(Controller *controller);
      void setCollider(Collider *collider);
  private:
      Texture *texture;
  };
  </code></pre>
  
  <p>where the getters and setters just call the relevant <code>Object</code> method. But in this case, I can not really use the <code>Sprite</code> as an <code>Object</code>, e.g. pass it to a function which takes <code>Object</code>s.</p>
  
  <p>My other implementation is:</p>
  
  <pre><code>class Sprite : public Object {
  public:
      Sprite(Texture *texture) : Object(), texture(texture) {
          setRenderer(new SpriteRenderer(this));
      }
  private:
      Texture *texture;
  };
  </code></pre>
  
  <p>which exposes the <code>setRenderer</code> method.</p>
  
  <p>My thought process is that the <code>setRenderer</code> method should not be exposed because a <code>Sprite</code> is an <code>Object</code> with a <code>SpriteRenderer</code>, so if it is changed, then it is not a <code>Sprite</code>. However, I do not see an obvious way to prevent it from changing.</p>
  
  <p>In general is this something I should just not worry about, or is there another way to go about this design?</p>