title: dynamic_cast of "this" inside constructor
body: <p>This question is very similar to this one <a href="https://stackoverflow.com/questions/4641398/why-cant-i-dynamic-cast-sideways-during-multiple-inheritence">Why can&#39;t I dynamic_cast &quot;sideways&quot; during multiple inheritence?</a>, except that the cast does work - just not inside in the constructor.</p>
  
  <p>Header:  </p>
  
  <pre><code>class A  
  {  
  public:  
      virtual                ~A() {}
      void                    printA();
  };
  
  class B
  {
  public:
                              B();
      virtual                ~B() {}
      void                    printB();
  
  private:
      std::string             message_;
  };
  
  class C : public A, public B
  {
  public:
                          C() {}
      virtual                ~C() {}
  };
  </code></pre>
  
  <p>Source:</p>
  
  <pre><code>void A::printA() { cout &lt;&lt; "A" &lt;&lt; endl; }
  B::B()
  {
      A* a = dynamic_cast&lt; A* &gt;( this );
      if ( a ) {
          message_ = std::string( "A and B" );
      } else {
          message_ = std::string( "B" );
      }
  }
  void B::printB() { cout &lt;&lt; message_.c_str() &lt;&lt; endl; }
  </code></pre>
  
  <p>Main:</p>
  
  <pre><code>int main( int argc, char* argv[] )
  {
      cout &lt;&lt; "Printing C..." &lt;&lt; endl;
      C c;
      c.printA();
      c.printB();
  
      cout &lt;&lt; "Checking again..." &lt;&lt; endl;
      cout &lt;&lt; !!dynamic_cast&lt; A* &gt;( &amp;c ) &lt;&lt; endl;
  
      return EXIT_SUCCESS;
  }
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>Printing C...
  A
  B
  Checking again...
  1
  </code></pre>
  
  <p>So, the dynamic_cast does work for multiple inheritance (no surprises there!), but why not when called at runtime for the 'this' pointer inside B::B()?  I thought that the object was fully formed once inside the body of the constructor i.e. all the memory was allocated for the component objects, they haven't been initialised yet.  I appreciate that this depends on the superclass constructor order, but in this example A is called before B.</p>
  
  <p>I am obviously not understanding what exactly is happening under the hood, can someone please enlighten me?</p>
  
  <p>Thanks,
  Cam Bamber.</p>