gpt4 book ai didi

C++拷贝构造函数

转载 作者:行者123 更新时间:2023-11-30 05:32:06 25 4
gpt4 key购买 nike

我试图很好地掌握复制构造函数,我已经找到了这部分代码。

 #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();为什么调用复制构造函数和构造函数而不是默认赋值运算符,我得到

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 :我刚刚发现这是通过复制构造函数进行的初始化,而不是通过赋值运算符进行的赋值。

最佳答案

让我们从它开始吧。

第一个;

你创建一个对象,它的字段(非静态成员)被初始化

"Before the compound statement that forms the function body of the constructor begins executing, initialization of all direct bases, virtual bases, and non-static data members is finished."

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)

data1A类型,复制first对象(这里引用为a )

一个新的 A 对象是由它自己的复制构造函数创建的。首先,它为A1调用复制构造函数,

A(const A& a) :
a1(a.a1)

I am copy constructing an A1

然后,Aa2a3 字段被默认初始化。

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 构造函数(采用 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

data2A&,方法返回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

关于程序终止

销毁以相反的顺序发生,最后创建的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 - 只有一次。后者是因为您没有采用 const & BigABigA 复制构造函数(上面也指出了这一点)。

回答您的问题

void change() {
A& rdata1 = data1;
A cdata2 = data2;
}
//in the main():
biga.change();

是的,你是对的,复制构造函数将在这里被调用 A cdata2 = data2; 因为对象 cdata2 之前未初始化。这种情况很好解释under this ref .

如果你这样修改代码

A cdata2;
cdata2 = data2;

你会看到预期的分配:

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 performing a stupid assignment between As

关于C++拷贝构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35268350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com