Destructors
A destructor is the member function that runs when an object's lifetime ends. It has the name ~Class, takes no arguments, and is where the object releases what it owns.
Detailed Explanation
A constructor turns raw storage into an object. A destructor turns the object back into raw storage. C++ calls the destructor automatically when a local leaves scope, when a delete expression runs, when a container erases an element, and when stack unwinding runs after a throw. You almost never call it yourself.
The default destructor destroys members and bases in reverse order of construction: last member first, then earlier members, then bases. If every resource the class owns is itself an object with a destructor (std::string, std::vector, std::unique_ptr, std::ifstream), the default is correct. That is the rule of zero. You write a destructor when the class owns a raw resource: a FILE*, a new allocation, a socket handle, a lock you forgot to wrap.
The destructor body runs first, then the members are destroyed. So if you need a member during cleanup (flush a buffer stored in a std::string before closing a raw FILE*), do that work in the body; the string is still alive. After the body returns, the string destructor runs.
Destructors are implicitly noexcept unless you declare otherwise. Throwing from a destructor is a process-ending event if another exception is already in flight: std::terminate is called. Even if no other exception is active, a throwing destructor makes containers and algorithms unsafe (vector cannot clean up on a failed push_back). Catch errors inside the destructor, or expose close() for callers who want to handle failure, and have the destructor call a non-throwing best-effort close.
A virtual destructor on a base class is required if you delete a derived object through a base pointer. Without it, delete base calls only the base destructor and the derived members leak. If a class is not used as a polymorphic base, leave the destructor non-virtual: a virtual destructor adds a vtable pointer to every object.
You cannot take the address of a destructor in portable code to call it later as a function pointer. Placement-new users call p->~T() explicitly to end the lifetime without freeing the storage. That is the exception to "never call it yourself", and it is paired with matching placement construction.
Order across a hierarchy: ~Derived body, then derived members, then ~Base body, then base members. Construction is the reverse. A member that must outlive another should be declared first so it is destroyed last.
Code Examples
#include <cstdio>
#include <stdexcept>
class File {
public:
explicit File(const char* path) : fp_(std::fopen(path, "w")) {
if (!fp_) throw std::runtime_error("open failed");
}
~File() {
if (fp_) std::fclose(fp_); // runs on return, break, and throw
}
File(const File&) = delete;
File& operator=(const File&) = delete;
void puts(const char* s) { std::fputs(s, fp_); }
private:
std::FILE* fp_;
};
int main() {
File out("/tmp/cpp-glossary-demo.txt");
out.puts("ok\n");
} // ~File closes the handle#include <iostream>
struct Base {
virtual ~Base() { std::cout << "Base\n"; }
};
struct Derived : Base {
~Derived() { std::cout << "Derived\n"; }
};
int main() {
Base* p = new Derived;
delete p; // prints: Derived then Base
}Common Gotchas
Non-virtual destructor plus `delete` through base
only `~Base` runs. Make `~Base` virtual, or do not delete through `Base*`.
Throwing from a destructor
implicit `noexcept` plus a throw calls `std::terminate`. Never let an exception escape `~T`.
A destructor does not run if construction failed
if the constructor throws, the object never existed. Members already constructed are destroyed. Raw handles acquired in the constructor body before the throw leak unless you clean them in the constructor or wrap each in a member.
Prefer the rule of zero
if you find yourself writing `~T` plus copy and move, first try storing the resource in `unique_ptr` with a custom deleter.