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

c++ - order of construction and destruction

我有以下 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;运行s,创建第三个对象的副本返回。 然后,在返回之前唯一被破坏的对象是第二个对象。同样,这是否与他们的创作有关,或者与 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) 在函数退出的那一刻就超出了范围。其他对象的生命周期稍长,因为它们是在函数外创建的,并由 reference/pointer 传递。它们一直存在到完整表达式的末尾 f(1, A(2), &A(3));,因此它们稍后会被破坏。