Which one do you prefer to delete objects? Especially in QT, but other practices are also welcome. These two alternatives seem same to me, are they?
-
Bound to another class, and destroy when it is destroyed.
SomeClass::SomeClass{ socket_ = new QTcpSocket(this); }
or
-
Destroy in the destructor of class
SomeClass::SomeClass{ socket_ = new QTcpSocket(); } SomeClass::~SomeClass{ delete socket_; }
When in Rome, do as the Romans do. If your framework uses one method (for example Qt relies on parent-child relationship), use this method in your code.
Of course don’t forget about general good memory management practices: create object on stack whenever it’s possible, use shared pointers, etc.