#include <iostream>
using namespace std;
class A
{
int x;
public:
A(int c) : x(c) {}
A(const A& a) { x = a.x; cout << "copy constructor called" << endl;}
};
class B
{
A a;
public:
B(int c) : a(c) {}
B(const B& b) : a(b.a) { }
A get_A() { return a;}
};
int main()
{
B b(10);
A a1 = b.get_A();
}
In the code above, I expected that 'copy constructor called' message would pop up twice because first, the b.get_A() will create a temporary object which invokes copy constructor (1) and second, it will copy its reference to a1 's copy constructor (2) , thus making two messages showing up.
However, the code actually results a single 'copy constructor called' message. Why?