gpt4 book ai didi

c++ - 构造和破坏的顺序

转载 作者:太空狗 更新时间:2023-10-29 20:21:17 25 4
gpt4 key购买 nike

我有以下 C++ 代码 (VS2013):

#include <iostream>
using namespace std;
class A {
int i;
public:
A(int i) : i(i) {
cout << "DEFAULT CTOR " << i << endl;
}
A(const A &o) : i(o.i) {
cout << "COPY CTOR " << i << endl;
}
~A() {
cout << "DTOR " << i << endl;
}
friend A f(const A &, A, A *);
};
A f(const A &a, A b, A *c) {
return *c;
}
int main() {
f(1, A(2), &A(3));
}

它产生以下输出:

DEFAULT CTOR 1

DEFAULT CTOR 3

DEFAULT CTOR 2

COPY CTOR 3

DTOR 2

DTOR 3

DTOR 3

DTOR 1

前 3 个是参数构造函数(错误地输出“DEFAULT CTOR”,但这无关紧要),它们在调用 f 之前被调用。

然后,当运行 return *c; 行时,运行值为 3 的复制构造函数,然后销毁值为 2 的对象。

最后,在 main 范围的末尾,剩余的对象 (3, 3, 1) 被销毁。

我不明白这种行为,也找不到任何解释。

谁能详细说明事情发生的顺序?

具体来说:

  1. 为什么第三个对象&A(3)构造在第二个对象A(2)之前?这和它们的创建有什么关系吗(第三个是引用,第二个是值),还是f的定义方式(第二个是值,第三个是指针) ?

  2. return *c; 运行时,将创建第三个对象的拷贝以供返回。然后,在返回之前唯一被破坏的对象是第二个对象。同样,这与它们的创建或 f 的定义方式有什么关系吗?

提前致谢。

最佳答案

Why is the third object &A(3) constructed before the second object A(2)? Does this have anything to do with their creation (the third is by reference, the second is by value), or with the way f is defined (the second is by value, the third is a pointer)?

这是因为 C++ 标准未指定函数参数的计算顺序。编译器可以随心所欲地评估它们。这是许多未定义行为实例的原因,当不知道这一点的程序员依赖于不存在的顺序时。

When return *c; runs, a copy of the third object is created to be returned. Then, the only object being destructed before returning is the second object. Again, does this have anything to do with their creation, or with the way f is defined?

有点,是的。 A(2) 对象是通过直接初始化函数 f 的参数创建的。函数参数的范围是函数体。所以 A(2) 在函数退出的那一刻就超出了范围。其他对象的生命周期稍长,因为它们是在函数外部创建的,并通过引用/指针传递。它们一直存在到完整表达式 f(1, A(2), &A(3)); 的末尾,因此它们稍后会被破坏。

关于c++ - 构造和破坏的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44900289/

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