Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A pure virtual destructor is a destructor declared with the pure-specifier (= 0) that renders its containing class abstract, preventing direct instantiation. Unlike standard pure virtual methods, a pure virtual destructor must be explicitly defined (implemented) in the program. When an object of a derived class is destroyed, the C++ destruction sequence dictates that destructors are invoked in the reverse order of construction: the derived class destructor executes first, followed implicitly by the base class destructor. Because the compiler automatically injects this call to the base destructor at the end of the derived destructor’s execution, the base class must provide a valid function body. If the definition is omitted, the compiler will accept the pure declaration, but the linker will generate an unresolved external symbol error. Crucially, unlike standard pure virtual functions, a derived class is not required to explicitly declare and define a destructor to become a concrete class. If the derived class omits an explicit destructor, the compiler-generated default destructor automatically overrides the base class’s pure virtual destructor and fulfills the requirement to instantiate the derived type.
class AbstractBase {
public:
    // Declaration: Pure specifier makes the class abstract
    virtual ~AbstractBase() = 0; 
};

// Definition: Mandatory implementation to satisfy the linker
AbstractBase::~AbstractBase() {
    // Base class cleanup logic (can be empty)
}

class Derived : public AbstractBase {
    // No explicit destructor required to make this class concrete.
    // The compiler-generated ~Derived() automatically overrides ~AbstractBase()
    // and implicitly calls AbstractBase::~AbstractBase().
};
Key Technical Characteristics:
  • Out-of-Class Definition: The definition of a pure virtual destructor cannot be provided inside the class body alongside the = 0 specifier. It must be defined outside the class definition. However, the implementation can still be marked with the inline keyword, provided the inline definition occurs outside the class body.
  • Abstract Enforcement: Applying = 0 to the destructor forces the class to be abstract even if it contains no other pure virtual functions.
  • Vtable Mechanics and Linkage: The pure virtual destructor occupies a slot in the virtual method table (vtable). The derived class overrides this slot with its own destructor. Because the derived destructor statically calls the base destructor at the end of its execution, the base implementation must exist somewhere in the program (in any translation unit) so the linker can successfully resolve the symbol.
  • Modern C++ (C++11+): The mandatory out-of-class definition can utilize the default specifier to instruct the compiler to generate a standard, empty implementation.
class ModernBase {
public:
    virtual ~ModernBase() = 0;
};

// Out-of-class definition using 'inline' and 'default'
inline ModernBase::~ModernBase() = default; 
Master C++ with Deep Grasping Methodology!Learn More