我试图抓住复制构造函数&我找到了这部分代码。
#include<iostream>
using namespace std;
class A1 {
int data;
public:
A1(int i = 10) :
data(i) {
cout << "I am constructing an A1 with: " << i << endl;
}
A1(const A1& a1) :
data(a1.data) {
cout << "I am copy constructing an A1" << endl;
}
~A1() {
cout << "I am destroying an A1 with: " << data << endl;
}
void change() {
data = data * 10;
}
};
class A2 {
int data;
public:
A2(int i = 20) :
data(i) {
cout << "I am constructing an A2 with: " << i << endl;
}
A2(const A2& a2) :
data(a2.data) {
cout << "I am copy constructing an A2" << endl;
}
~A2() {
cout << "I am destroying an A2 with: " << data << endl;
}
void change() {
data = data * 20;
}
};
class A3 {
public:
A3() {
cout << "I am constructing an A3" << endl;
}
A3(const A3& a3) {
cout << "I am copy constructing an A3" << endl;
}
~A3() {
cout << "I am destroying an A3" << endl;
}
void change() {
cout << "Nothing to change" << endl;
}
};
class A {
A1 a1;
A2 a2;
A3 a3;
public:
A() {
cout << "I am constructing an A" << endl;
}
A(const A& a) :
a1(a.a1) {
cout << "I am copy constructing an A" << endl;
}
~A() {
cout << "I am destroying an A" << endl;
}
A& operator=(const A& a) {
cout << "I am performing a stupid assignment between As" << endl;
if (this != &a)
a1 = a.a1;
return *this;
}
void change() {
a1.change();
a2.change();
a3.change();
}
};
class BigA {
A data1;
A& data2;
public:
BigA(A& a) :
data1(a), data2(a) {
cout << "I just constructed a BigA" << endl;
}
~BigA() {
cout << "I am destroying a BigA" << endl;
}
A get(int index) {
if (index == 1)
return data1;
else
return data2;
}
};
BigA volta(BigA& biga)
//BigA& volta(BigA& biga)
{
cout << "Volta ta data?" << endl;
return biga;
}
int main() {
A first;
BigA biga(first);
volta(biga).get(2).change();
return 0;
}
然而,我无法理解为什么我得到这些结果。特别是,为什么A1和A复制构造函数被调用而不是构造函数,当调用volta函数时我根本没有得到(由***括起来的结果) *):
I am constructing an A1 with: 10
I am constructing an A2 with: 20
I am constructing an A3
I am constructing an A
I am copy constructing an A1
I am constructing an A2 with: 20
I am constructing an A3
I am copy constructing an A
I just constructed a BigA
****
Volta ta data?
I am copy constructing an A1
I am constructing an A2 with: 20
I am constructing an A3
I am copy constructing an A
I am copy constructing an A1
I am constructing an A2 with: 20
I am constructing an A3
I am copy constructing an A
Nothing to change
I am destroying an A
I am destroying an A3
I am destroying an A2 with: 400
I am destroying an A1 with: 100
I am destroying a BigA
I am destroying an A
I am destroying an A3
I am destroying an A2 with: 20
I am destroying an A1 with: 10
****
I am destroying a BigA
I am destroying an A
I am destroying an A3
I am destroying an A2 with: 20
I am destroying an A1 with: 10
I am destroying an A
I am destroying an A3
I am destroying an A2 with: 20
I am destroying an A1 with: 10
EDIT_AssignmentOperatorQuery:如果我在BigA中添加此功能
void change() {
A& rdata1 = data1;
A cdata2 = data2;
}
并从main调用它:biga.change();
为什么而不是默认赋值运算符,正在调用copy-constructor和constructor,我得到了
I am copy constructing an A1
I am constructing an A2 with: 20
I am constructing an A3
I am copy constructing an A
EDIT_AnsweringMyOwnQuery:我刚刚发现这是复制构造函数初始化而不是赋值运算符赋值。
答案 0 :(得分:1)
让我们从它开始。
A first;
您创建一个对象,其字段(非静态成员)已初始化
I am constructing an A1 with: 10
I am constructing an A2 with: 20
I am constructing an A3
正在调用没有参数的构造函数版本:
I am constructing an A
写作时
BigA biga(first);
调用了一个BigA
构造函数。它需要引用A
对象,因此,first
不会被复制(在提供值时会设置引用)。
然后,成员初始化器列出时间到来,
BigA(A& a) :
data1(a), data2(a)
且data1
的类型为A
,first
对象已被复制(此处引用为a
)
新的A
对象由其自己的复制构造函数创建。首先,它调用A1
的复制构造函数,
A(const A& a) :
a1(a.a1)
I am copy constructing an A1
然后,A
的{{1}}和a2
字段默认初始化。
a3
然后执行I am constructing an A2 with: 20
I am constructing an A3
的复制构造函数体:
A1
让我们回到I am copy constructing an A
初始化。到目前为止,我们谈到BigA
初始化,现在是data1
的时间:
A& data2
由于它是引用,并且传递引用以初始化它,它只是一个赋值,没有输出。
然后执行 BigA(A& a) :
data1(a), data2(a)
构造函数(需要BigA
)body:
A&
现在,我们将尝试澄清
上发生的事情I just constructed a BigA
正在调用此函数:
volta(biga).get(2).change();
再次,通过引用传递没有复制构造函数调用。
我们正在执行功能体:
BigA volta(BigA& biga)
{
cout << "Volta ta data?" << endl;
return biga;
}
该函数返回类"Volta ta data?"
的未命名对象,因此应该调用复制构造函数。
您尚未提供BigA
之类的复制构造函数,因此正在调用默认复制构造函数。
它执行BigA (const BigA & biga)
然后A data1;
通过复制未命名对象的A& data2;
字段来初始化第一个成员,因此正在调用data1
的复制构造函数。以上解释了此处打印的内容(请参阅:新的A
对象由其自己的复制构造函数创建... )
A
然后,I am copy constructing an A1
I am constructing an A2 with: 20
I am constructing an A3
I am copy constructing an A
方法与get
index == 2
A get(int index) {
if (index == 1)
return data1;
else
return data2; // <--- this line is executed
为data2
,方法返回A&
,这会导致A
复制构造函数执行。
A
最后,I am copy constructing an A1
I am constructing an A2 with: 20
I am constructing an A3
I am copy constructing an A
运行
change
并且仅void change() {
a1.change();
a2.change();
a3.change();
}
打印出一些内容:
a3.change()
破坏以相反的顺序发生,最后创建的Nothing to change
'd对象首先被销毁。
change
I am destroying an A
I am destroying an A3
I am destroying an A2 with: 400
I am destroying an A1 with: 100
打印两次,但I am destroying a BigA
- 仅打印一次。后者是因为你没有I just constructed a BigA
的{{1}}复制构造函数(上面也有人指出)。
BigA
是的,你在这里调用复制构造函数const & BigA
是对的,因为对象void change() {
A& rdata1 = data1;
A cdata2 = data2;
}
//in the main():
biga.change();
以前是未初始化的。这种情况很好地解释了under this ref。
如果你改变那样的代码
A cdata2 = data2;
你会看到预期的任务:
cdata2