【发布时间】:2021-04-20 05:27:15
【问题描述】:
代码是:
#include <iostream>
using namespace std;
class A
{
public:
A() { cout << "A::A" << endl; }
~A() { cout << "A::~" << endl; }
};
class B
{
public:
B() { cout << "B::B" << endl; }
~B() { cout << "B::~" << endl; }
};
int main()
{
B b;
static A a;
return 0;
}
输出是:
B::B
A::A
B::~
A::~
非静态对象b的范围和静态对象a的范围在main()函数的末尾结束。
问:为什么构造函数的顺序和析构函数的顺序一样?
【问题讨论】:
标签: c++ constructor static destructor