I have a program below, and I'm trying to understand the following:
Why we don't enter the copy constructor when we have the temp' object C(...) that is the parameter for c1. I would also like to know how to identify this cases i.e when do we use the copy constructor and when not.
The program is:
#include<iostream>
using namespace std;
template <class T>
class A
{
public:
T* t;
A(T& obj) :t(&obj)
{
cout << "A::ctor" << endl;
}
};
class B
{
public:
int x1;
B(int x) :x1(x)
{
cout << "B::ctor" << endl;
}
};
class C
{
public:
A<B> a;
C(B& b) : a(b)
{
cout << "C::ctor" << endl;
}
C(const C& otherC) : a(otherC.a)
{
cout << "C::copy-ctor" << endl;
}
};
int main(void)
{
C c1( C( B(30) ) );
/*
//The way i see it, which is wrong
//Of course in this way b and c should be deleted
//some how (magically..) before using c1
B b(30);
C c(b);
C c1(c);
*/
return 0;
}