C++表示爲使用複合類的類創建複製構造函數,編譯器遞歸調用所有成員對象的複製構造函數。我想,在下面的代碼同樣的事情:複製成員對象的構造函數調用
class A
{
public:
A(){cout<<"A constructor called"<<endl;}
A(const A&){cout<<"A copy constructor called"<<endl;}
};
class B
{
public:
B(){cout<<"B constructor called"<<endl;}
B(const B&){cout<<"B copy constructor called"<<endl;}
};
class C
{
A a;
B b;
public:
C(){cout<<"C constructor called"<<endl;}
C(const C&){cout<<"C copy constructor called"<<endl;}// If you comment this line, you will get output: Case 1 (see below) and if you don't comment, you will get o/p: case 2(see below)
};
void main()
{
C c;
cout<<endl;
C c2 = c;
}`
案例1:
稱爲構造
稱爲
ç構造稱爲
複製構造乙構造叫做
乙稱爲
案例拷貝構造2:
稱爲構造
稱爲
Ç構造稱爲B構造
稱爲
乙常量的構造方法ructor稱爲
稱爲C拷貝構造
我的疑問是,對殼體2的O/P應該是:A,B,C,構造稱爲然後.. A,B,稱爲C拷貝構造。但它沒有發生。請幫忙。
閱讀關於RVO。 – Nawaz 2012-02-18 07:59:49
@ serge:我正在創建一個叫做構造函數,叫做B構造函數,叫做C複製構造函數。 – Jatin 2012-02-18 08:01:08