我有一个程序如下,我正在尝试理解以下内容:
当我们有临时'对象C(...)作为c1的参数时,为什么我们不进入复制构造函数。我还想知道如何识别这种情况,即我们何时使用复制构造函数,何时不使用。
该计划是:
#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;
}