Copy constructor invocation for member objects
<p>C++ says that to create a copy constructor for a class that uses composition, the compiler recursively calls the copy constructors for all the member objects. I tried that same thing in the below code:</p>

<pre><code>class A
{
public:
    A(){cout&lt;&lt;"A constructor called"&lt;&lt;endl;}
    A(const A&amp;){cout&lt;&lt;"A copy constructor called"&lt;&lt;endl;}
};

class B
{
public:
    B(){cout&lt;&lt;"B constructor called"&lt;&lt;endl;}
    B(const B&amp;){cout&lt;&lt;"B copy constructor called"&lt;&lt;endl;}
};

class C
{
    A a;
    B b;
public:
    C(){cout&lt;&lt;"C constructor called"&lt;&lt;endl;}
    C(const C&amp;){cout&lt;&lt;"C copy constructor called"&lt;&lt;endl;}// If you comment this line, you will get output: Case 1 (see below)     and if you don't comment, you will get o/p: case 2(see below)
};


void main()
{
    C c;
    cout&lt;&lt;endl;
    C c2 = c;
}`
</code></pre>

<p>Case 1:</p>

<blockquote>
  <p>A constructor called</p>
  
  <p>B constructor called </p>
  
  <p>C constructor called</p>
  
  <p>A copy constructor called</p>
  
  <p>B copy constructor called</p>
</blockquote>

<p>Case 2:</p>

<blockquote>
  <p>A constructor called</p>
  
  <p>B constructor called </p>
  
  <p>C constructor called</p>
  
  <p>A constructor called</p>
  
  <p>B constructor called</p>
  
  <p>C copy constructor called</p>
</blockquote>

<p>My doubt is that the o/p for case 2 should be: A, B, C, constructor called and then.. A, B, C copy constructor called. But it is not happening. Please help.</p>
