title: Multiple inheritance casting from base class to a different derived class
body: <p>I have got the following set of classes:</p>
  
  <p><img src="https://i.stack.imgur.com/pKq0E.png" alt="enter image description here"></p>
  
  <p>And the following piece of code:</p>
  
  <pre><code>A* a;
  if(condition)
  {
      a = new E();
  }
  else
  {
      a = new D();
  }
  </code></pre>
  
  <p>Now given that there is such function as <code>F::foo()</code>, in order to call it, I have to cast <code>a</code> to either <code>E*</code> or <code>D*</code>:</p>
  
  <pre><code>if(condition)
  {
      ((E*)a)-&gt;foo();
  }
  else
  {
      ((D*)a)-&gt;foo();
  }
  </code></pre>
  
  <p>As far as I know, casting <code>a</code> to <code>F*</code> in order to call <code>F::foo</code> would be illegal since <code>a</code> is of type <code>A*</code>; and to me, checking a condition before calling <code>foo</code>, sounds like a design problem. Could somebody please give some advice as to how I can improve this class hierarchy? </p>
  
  <p>p.s. Used <a href="http://yuml.me/diagram/scruffy/class/draw" rel="nofollow noreferrer">this tool</a> to draw the diagram.</p>