#include <iostream>
struct A
{
A() { std::cout << "\tA::default_constructor\n\n" ; }
A( const A& ) { std::cout << "\tA::copy_constructor\n\n" ; }
};
void foo( A ) {}
A bar() { A temp ; return temp ; }
int main()
{
std::cout << "default-initialize an A\n" ; A a1 ; // default constructor of A
std::cout << "make a copy of A\n" ; A a2 = a1 ; // copy constructor of A ;
std::cout << "pass A by value\n" ; foo(a1) ; // copy constructor of A ;
std::cout << "return A by value\n" ; A a3 = bar() ; // default constructor of A (NRVO)
struct B { A a ; } ;
std::cout << "default-initialize a B\n" ; B b1 ; // default constructor of A
std::cout << "make a copy of B\n" ; B b2 = b1 ; // copy constructor of A
}
Output:
default-initialize an A
A::default_constructor
make a copy of A
A::copy_constructor
pass A by value
A::copy_constructor
return A by value
A::default_constructor
default-initialize a B
A::default_constructor
make a copy of B
A::copy_constructor