title: c++ multiple inheritance casting
body: <p>I have a class:</p>
  
  <pre><code>class Base;
  </code></pre>
  
  <p>Also I have an interface</p>
  
  <pre><code>class Interface;
  </code></pre>
  
  <p>Next i'm creating a class</p>
  
  <pre><code>class Derived : public Base, public Interface;
  </code></pre>
  
  <p>If I have <code>Base *object = new Derived;</code></p>
  
  <p>How can i cast <code>object</code> to <code>Interface</code> ? (of course if i know than object is actually a derived class)</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I've tried dynamic_cast and static_cast (not compiled).
  So let me explain the problem a bit more:</p>
  
  <p>I have:</p>
  
  <pre><code>class Object {...}
  
  class ITouchResponder
  {
  public:
      virtual bool onTouchBegan(XTouch *touch) = 0;
      virtual void onTouchMoved(XTouch *touch) = 0;
      virtual void onTouchEnded(XTouch *touch) = 0;
  };
  
  class Ball : public Object, public ITouchResponder {...};
  
  class TentacleSensor : public Object, public ITouchResponder {...}
  </code></pre>
  
  <p>Object have a <code>bool touchable_</code> property. If it's true then object is implementing ITouchResponder interface.</p>
  
  <p>When I use it:</p>
  
  <pre><code>bool Level::onTouchBegan(XTouch *touch)
  {
      ...
      ITouchResponder *responder = callback.nearestTouchable();
      if (responder)
      {
          if (responder-&gt;onTouchBegan(touch))
          {
              if (responder != ball_)
              {
                  touch-&gt;setUserData(responder);
              }
          }
      }
  
      return true;
  }
  
  ITouchResponder *QueryCallback::nearestTouchable() const
  {
      for (list&lt;Object*&gt;::const_iterator it = objects_.begin(); it != objects_.end(); ++it)
      {
          if ( (*it)-&gt;isTouchable() ) return (*it)-&gt;asTouchResponder();
      }
      return 0;
  }
  </code></pre>
  
  <p><code>asTouchResponder</code> is a method of <code>Object</code> :</p>
  
  <pre><code>    ITouchResponder * Object::asTouchResponder()
      {
          assert(touchable_);
          ITouchResponder *res = dynamic_cast&lt;ITouchResponder*&gt;(this);
          assert(res);
          return res;
      }
  </code></pre>
  
  <p>I have bad excess error in xcode. </p>
  
  <p>But if i make <code>Object : public ITouchResponder</code> everything works fine. What am i doing wrong ?</p>
  
  <p>Full object class:</p>
  
  <pre><code>class Object// : public ITouchResponder
  {
  public:
      struct Def
      {
          Def()
          {
              level = 0;
              world = 0;
              touchable = false;
              acceptsContacts = false;
              body = 0;
              node = 0;
          }
          Level *level;
          b2World *world;
          bool touchable;
          bool acceptsContacts;
          b2Body *body;
          XNode *node;
      };
  
      Object(const Def &amp;def);
      virtual ~Object();
  
      virtual void update(float dt);
      bool isTouchable() const {return touchable_;}
  
      void addDependantObject(Object *object);
      void removeDependantObject(Object *object);
  
      virtual void objectWillBeRemoved(Object *object) {} //this function is automatically called to every dependant object when object is removed
  
      virtual XVec2 position() const;
      virtual float rotation() const;
  
      bool acceptsContacts() const {return acceptsContacts_;}
  
      b2Body *body() const {return body_;}
  
      Level *level() const {return level_;}
      b2World *world() const {return world_;}
  
      ITouchResponder *asTouchResponder();
      /*
      virtual bool onTouchBegan(XTouch *touch) {
          return false;
      }
      virtual void onTouchMoved(XTouch *touch)
      {
      }
      virtual void onTouchEnded(XTouch *touch) 
      {
      }*/
  
  protected:
      Level *level_;
      b2World *world_;
      bool touchable_;
      bool acceptsContacts_;
  
      XNode *node_;
      b2Body *body_;
  
      list&lt;Object*&gt; dependantObjects_;
  };
  </code></pre>