【发布时间】:2016-07-06 23:11:33
【问题描述】:
我不明白 Qt 如何在静态分配的情况下删除所有 QObject 的子对象而不重复删除。
基本上,如果我按照通常的方式进行操作,它看起来像这样:
QWidget Window(nullptr);
QPushButton* button = new QPushButton(&Window);
Window.show();
return App.exec();
//When app ends, Window gets deleted
//because it was statically allocated
//And then, Window deletes button because it's its child.
但我也可以做到这一点而不会崩溃:
QWidget Window(nullptr);
QPushButton button(&Window);
Window.show();
return App.exec();
//When app ends, button then Window get deleted
//because they were statically allocated
//And then, Window (should) delete button AGAIN because it's its child, thus crashing
//the program. But it doesn't. Why ?
Qt 知道我是如何创建 QPushButton 的吗,还是我错过了什么?
【问题讨论】:
-
是的,你确实错过了一些东西,即未定义的行为是未定义的。
-
@n.m.:据我所知,这里没有任何未定义的内容。
-
@MattiVirkkunen 在这个例子中可能没有什么未定义的,我只是说没有崩溃证明什么。
-
@n.m.:那你可能想说那句话而不是说一些无关紧要的话。