Delete object: use parent or not.

Which one do you prefer to delete objects? Especially in QT, but other methods are also welcome. These two alternatives seem the same to me, right?

  • Associated with another class and destroys when it is destroyed.

    SomeClass::SomeClass{
     socket_ = new QTcpSocket(this);
    }
    

or

  1. Destroy in class destructor

    SomeClass::SomeClass{
     socket_ = new QTcpSocket();
    }
    
    SomeClass::~SomeClass{
     delete socket_;
    }
    
+3
source share
3 answers

When in Rome do what the Romans do. If your framework uses one method (for example, Qt relies on the relationship between parents and children), use this method in your code.

, : , , ..

+8

RAII , SomeClass. SomeClass QTcpSocket, . , , , delete this.

+3

, , (1), ​​, Qt ( ) .

0

Source: https://habr.com/ru/post/1743970/


All Articles