First, when you want to free the memory assigned to an object in C++, which one is preferred? Explicitly calling destructor or using delete?
Object* object = new Object(...);
...
delete object;
OR
object->~Object();
Second, does the delete operator call the destructor implicitly?
deleteimplicitly calls the destructor, you don’t need (more precisely: shouldn’t) call it directly.A destructor will never release the memory occupied by the object (It may reside on the stack, not on the heap, and the object has no way of knowing — however, the destructor will delete any memory allocated by the object’s components).
In order to free the memory of an object allocated on the heap, you must call
delete.When you write your own class, C++ will provide a default destructor to free the memory allocated by component objects (such as a
QStringthat is a member of your class), but if you explicitly allocate memory (or other resources) in your constructor, be sure to provide a destructor that will explicitly free these resources.Another general rule regarding your own classes: If you mark any methods
virtual, your destructor should bevirtual, as well (even if you rely on the default destructor), so that the correct destructor is called for any classes derived from yours.