I have trouble understanding output of following code.
#include "stdafx.h"
#include <iostream>
using namespace std;
class A
{
public:
A() { cout << "A()" << endl; }
~A() { cout << "~A()" << endl; }
};
class B : public A
{
public:
B() { cout << "B()" << endl; }
~B() { cout << "~B()" << endl; }
};
class C : public B
{
public:
C() { cout << "C()" << endl; }
~C() { cout << "~C()" << endl; }
};
class D : public C
{
public:
D() { cout << "D()" << endl; }
~D() { cout << "~D()" << endl; }
};
const B& returnConstB(B& b) { return b; }
B returnB(B b) { return b; }
A returnA(A a) { return a; }
int main()
{
D d;
returnA(returnB(returnB(returnConstB(d))));
cin.get();
return 0;
}
At first, I have A,B,C,D constructors called when instantiating object d. After calling these couple of functions, the output is following:
~B()
~A()
~B()
~A()
~A()
~A()
~A()
~B()
~A()
~B()
~A()
I understand that there are copy constructors involved while passing a parameter by value and returning the value, but I can't quite understand when and where are these objects being created and destroyed in this case. Thanks in advance!