title: Why does using a virtual base class change the behavior of the copy constructor
body: <p>In the following program the <code>a</code> member variable is not copied when B is virtually derived from A and instances of C (not B) are copied.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  class A {
  public:
      A() { a = 0; printf("A()\n"); }
  
      int a;
  };
  
  class B : virtual public A {
  };
  
  class C : public B {
  public:
      C() {}
      C(const C &amp;from) : B(from) {}
  };
  
  template&lt;typename T&gt;
  void
  test() {
      T t1;
      t1.a = 3;
      printf("pre-copy\n");
      T t2(t1);
      printf("post-copy\n");
      printf("t1.a=%d\n", t1.a);
      printf("t2.a=%d\n", t2.a);
  }
  
  int
  main() {
      printf("B:\n");
      test&lt;B&gt;();
  
      printf("\n");
  
      printf("C:\n");
      test&lt;C&gt;();
  }
  </code></pre>
  
  <p>output:</p>
  
  <pre><code>B:
  A()
  pre-copy
  post-copy
  t1.a=3
  t2.a=3
  
  C:
  A()
  pre-copy
  A()
  post-copy
  t1.a=3
  t2.a=0
  </code></pre>
  
  <p>Note that if B is normally derived from A (you delete the <code>virtual</code>) then <code>a</code> is copied.</p>
  
  <p>Why isn't <code>a</code> copied in the first case (<code>test&lt;C&gt;()</code> with B virtually derived from A?</p>