Destructors in C++
Destructors in C++ are special member functions that handle cleanup when an object goes out of scope or is explicitly deleted. They release resources such as memory, file handles, or network connections and perform necessary teardown tasks.
If a destructor is not declared, the compiler provides a default one. For simple classes without resource management, the default is usually sufficient.
A destructor should generally be public and virtual, or protected and non-virtual. Classes that manage resources often need explicit cleanup before destruction.
Destructors and Access Specifiers
Virtual Destructors
class Base {
public:
virtual ~Base() { std::cout << "Base destroyed\n"; }
};
class Derived : public Base {
public:
~Derived() { std::cout << "Derived destroyed\n"; }
};
Base* ptr = new Derived();
delete ptr; // Calls Derived::~Derived(), then Base::~Base()
Without a virtual destructor, only `Base::~Base()` would be called, causing incomplete cleanup.
Protected Destructors
Use a protected, non-virtual destructor when the class is intended as a base but not meant for polymorphic deletion. This prevents accidental deletion through base pointers.
class NonPolymorphicBase {
protected:
~NonPolymorphicBase() = default;
};
class Derived : public NonPolymorphicBase {
public:
~Derived() { std::cout << "Derived destroyed\n"; }
};
Derived d; // OK
NonPolymorphicBase* b = &d;
// delete b; // Error: destructor is protected
Public Destructors
Use a public destructor when objects are meant to be created and destroyed normally and no polymorphism is involved.
class Regular {
public:
~Regular() { std::cout << "Destroyed\n"; }
};
| Scenario | Destructor Type | Rationale |
|---|---|---|
| Class used polymorphically | `public virtual ~Class()` | Ensures correct destruction through base pointer |
| Base class not for polymorphic deletion | `protected ~Class()` | Prevents unsafe deletion through base pointer |
| Concrete, standalone class | `public ~Class()` | Normal destruction, no polymorphism needed |