我不明白Qt如何删除所有QObject的子元素,如果它是静态分配的话,没有双重删除。Qt如何处理堆栈分配的对象
基本上,如果我这样做,通常的方式,它看起来像这样:
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在这个例子中可能没有定义,我只是说没有崩溃证明没有任何证据。 –