A destructor in C++ is a special member function, named ~ClassName(), that the compiler calls automatically when an object's lifetime ends. It releases whatever the object owns, such as heap memory, file handles, sockets or locks. Destructors are the foundation of RAII (Resource Acquisition Is Initialization), the idiom that makes C++ code leak-free and exception-safe.
A destructor has no return type, no parameters and cannot be overloaded, so a class has at most one. If you do not write one, the compiler generates a default that destroys the members.
The destructor is called at these moments:
return.delete is applied to a pointer to a heap object, or delete[] to an array.std::unique_ptr releases the last owner.Objects are destroyed in reverse order of construction, and b is destroyed as soon as its block closes.
A class that owns a raw resource must release it in its destructor.
Because the file is closed in the destructor, it is closed on every path out of the scope, including exceptions. Copying is deleted because two objects closing the same handle would be a bug, which leads to the next section.
If a class needs a user-defined destructor, copy constructor, copy assignment, move constructor or move assignment, it probably needs to think about all five. A destructor that frees a resource means the default copy would produce two objects freeing the same pointer (a double free). Either implement the other four correctly, delete them, or avoid the problem entirely with the rule of zero.
The rule of zero says to hold resources in members that already manage themselves (std::string, std::vector, std::unique_ptr, std::fstream), so you write no destructor at all.
Prefer this style in modern C++. Write a manual destructor only when wrapping a C API or a custom resource.
If you delete a derived object through a base class pointer, the base class destructor must be virtual. Otherwise the derived destructor is skipped, which is undefined behaviour.
Any class intended as a polymorphic base should declare a public virtual destructor, or a protected non-virtual one if it must never be deleted through the base.
Destructors are implicitly noexcept since C++11. If a destructor throws while the program is already unwinding because of another exception, std::terminate is called. Catch and handle errors inside the destructor instead of letting them escape. If an operation can fail meaningfully, such as flushing a buffer, offer an explicit close() method that can report the error and let the destructor act as a safety net.
Yes. A private destructor prevents creating the object on the stack or deleting it from outside, which is used to force heap allocation or controlled lifetimes. A protected destructor is common in base classes.
Almost never. The compiler calls it for you. Explicit calls are only needed with placement new, where you construct an object in pre-allocated storage and must destroy it yourself.
delete?delete calls the destructor and then frees the memory. The destructor alone only cleans up the object's resources and does not release the object's own storage.
= default do on a destructor?It asks the compiler to generate the standard destructor explicitly. It is typically used to declare a virtual destructor with the default behaviour.
A C++ destructor, ~ClassName(), runs automatically at the end of an object's lifetime and releases its resources. Use it for RAII, follow the rule of zero or five, make base class destructors virtual, and never let exceptions escape from it.