Let's have a class Test
and a class AnotherClass
. Both derive from QObject.
Test.h:
class Test : public QObject
{
Q_OBJECT
public:
Test(QObject* parent);
~Test();
private:
AnotherClass* other;
};
class AnotherClass : public QObject
{
Q_OBJECT
public:
AnotherClass(QObject* parent);
~AnotherClass();
};
Test.cpp:
Test::Test(QObject* parent) : QObject(parent)
{
other = new AnotherClass(this);
}
Test::~Test()
{
delete other;
}
other
should be automatically destroyed when the Test
-instance is destroyed because Test
is the parent of other
, right?
- Now should I delete
other
by myself in~Test()
or does it leave the program in an undefined stage because it tries to delete the same object twice? - What is the correct approach here?
delete this
in your code after deletingother
??? Are QObjects automatically deleted without explicitdelete
word even if they have no parents?