Код: Выделить всё
class Child
{
public:
Child() {}
~Child() {}
};
class Parent : public QObject
{
Q_OBJECT
public:
explicit Parent(QObject *parent = 0): QObject(parent) {}
~Parent()
{
qDeleteAll(m_children);
}
void addChild(Child *ch)
{
m_children.append(ch);
}
private:
QList m_children;
};
Код: Выделить всё
ChildЭкземпляры
Следующее использование приведет к двойному уничтожению дочернего элемента:
Код: Выделить всё
int main()
{
{
Parent father;
Child child;
father.addChild( &child );
}
//child is out of scope now and gets destroyed
//father gets destroyed too
//father's destructor deletes child AGAIN!
//crash!
return 0;
}
Подробнее здесь: https://stackoverflow.com/questions/262 ... t-pointers