title: Does passing reference to itself to constructor of base class causes undefined behavior?
body: <p>while implementing some design ideas I stumbled upon some strange behavior, which I cannot explain myself.</p>
  
  <p>Take a look at the following example, which has been compiled with MSVC2010.</p>
  
  <pre><code>struct A
  {
      void helloMe()
      {
          std::cout &lt;&lt; "hej da!\n";
      }
  };
  
  struct B
  {
      B(A&amp; a): 
          m_a(a)
          {
              std::cout &lt;&lt; "B got constructed!\n"; 
          }
  
  A&amp; m_a;
  };
  
  struct C : A, B 
  {
      C() : 
          A(),
          B(*this)
          { std::cout &lt;&lt; "C got constructed!\n";
          }
  };
  
  int _tmain() 
  {
  
      C c;
      c.m_a.helloMe();
  
      std::cin.get();
      return 0;
  }
  </code></pre>
  
  <p>This compiles error free, but when executing, the constructor of B is never called. That is, when trying to access <code>m_a</code>, I got an segfault. But when explicitly casting to have <code>*reinterpret_cast&lt;A*&gt;(this)</code>, the constructors are called as intended.</p>
  
  <p>So why does the above example not work, but I need to explicitly cast? Is this a compiler dependent issue?</p>
  
  <p>EDIT: </p>
  
  <p>So with cast it would look like:</p>
  
  <pre><code>struct A
  {
      void helloMe()
      {
          std::cout &lt;&lt; "hej da!\n";
      }
  };
  
  struct B
  {
      B(A&amp; a): 
          m_a(a)
          {
              std::cout &lt;&lt; "B got constructed!\n"; 
          }
  
  A&amp; m_a;
  };
  
  struct C : A, B 
  {
      C() : 
          A(),
          B(*reinterpret_cast&lt;A*&gt;(this))
          { std::cout &lt;&lt; "C got constructed!\n";
          }
  };
  
  int _tmain() 
  {
  
      C c;
      c.m_a.helloMe();
  
      std::cin.get();
      return 0;
  }
  </code></pre>