> ## 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.

# C++ Virtual Destructor

A virtual destructor is a destructor in a base class declared with the `virtual` keyword. It enables dynamic dispatch for object destruction, ensuring that the most derived class's destructor is invoked when an object is deleted through a pointer to its base class.

## Syntax

```cpp theme={"dark"}
class Base {
public:
    virtual ~Base(); // Virtual destructor declaration
};
```

## Mechanics and Execution Flow

When a class declares a virtual destructor, the compiler integrates it into the Virtual Method Table (vtable). Upon invoking the `delete` operator on a base class pointer, the runtime environment resolves the destructor call dynamically through the vtable rather than statically based on the pointer's type.

This guarantees the standard C++ destruction sequence:

1. The most derived destructor executes.
2. Class members are destroyed in reverse order of their initialization.
3. The base class destructor executes.

## Static vs. Dynamic Binding

Without the `virtual` keyword, the destructor call is statically bound at compile time. Deleting a derived object via a base pointer with a non-virtual destructor results in **Undefined Behavior (UB)** according to the C++ Standard. Typically, this manifests as only the base class destructor executing, bypassing the derived destructor entirely.

```cpp theme={"dark"}
// Static Binding (Non-Virtual)
class BaseStatic {
public:
    ~BaseStatic() {} 
};
class DerivedStatic : public BaseStatic {
public:
    ~DerivedStatic() {}
};

// Dynamic Binding (Virtual)
class BaseDynamic {
public:
    virtual ~BaseDynamic() {} 
};
class DerivedDynamic : public BaseDynamic {
public:
    ~DerivedDynamic() override {}
};

int main() {
    BaseStatic* ptr1 = new DerivedStatic();
    delete ptr1; // UNDEFINED BEHAVIOR: Only ~BaseStatic() is invoked.

    BaseDynamic* ptr2 = new DerivedDynamic();
    delete ptr2; // WELL-DEFINED: ~DerivedDynamic() executes, followed by ~BaseDynamic().
    
    return 0;
}
```

## Pure Virtual Destructors

A destructor can be declared as pure virtual (`= 0`) to force the base class to become an abstract class, preventing direct instantiation. However, unlike standard pure virtual methods, a pure virtual destructor **must** have an out-of-line definition.

Because the destruction sequence inherently propagates up the inheritance chain, the derived class's destructor will implicitly invoke the base class's destructor. If the pure virtual destructor lacks a definition, the linker will throw an unresolved external symbol error.

```cpp theme={"dark"}
class AbstractBase {
public:
    virtual ~AbstractBase() = 0; // Pure virtual declaration
};

// Mandatory definition
AbstractBase::~AbstractBase() {
    // Base destruction logic, if any
}
```

## Architectural Rule

In C++ class design, the presence of a virtual destructor is tied to polymorphism. The standard technical guideline (C++ Core Guideline C.35) dictates: **A base class destructor should be either public and virtual, or protected and non-virtual.**

* **Public and Virtual:** Allows the object to be safely destroyed polymorphically via a base class pointer.
* **Protected and Non-Virtual:** Prevents polymorphic deletion entirely. If code attempts to `delete` a derived object through a base pointer, the compiler will reject it, safely avoiding Undefined Behavior at compile time without incurring the overhead of a virtual destructor.

Conversely, if a class is not intended to be used as a base class at all, the destructor should remain public and non-virtual to avoid the memory overhead of a vptr (virtual pointer).

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C++ Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
