【发布时间】:2017-12-30 05:17:34
【问题描述】:
我有示例代码:
#include <QCoreApplication>
#include <QTimer>
#include <cstdio>
using namespace std;
class A : public QObject
{
public:
~A() { printf("destructor A\n"); }
};
class B : public QObject
{
public:
B(QObject * parent) : QObject(parent) { }
~B() { printf("destructor B\n"); }
};
class C : public QObject
{
public:
C(QObject * parent) : QObject(parent) { }
~C() { printf("destructor C\n"); }
};
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
auto * a = new A;
auto * b = new B(a);
auto * c = new C(a);
delete a;
QTimer::singleShot(30000, &app, &QCoreApplication::quit);
return app.exec();
}
上述应用程序的执行给了我这个结果:
destructor A
destructor B
destructor C
auto(qt) 销毁操作的顺序是什么?
我可以假设破坏顺序(B,C)与创建顺序(新 B(a),新 C(a))相同吗?
为什么破坏顺序不是:A、C、B?
【问题讨论】: