In C++, what makes a class abstract, can a pure virtual function have a definition, and why must a pure virtual destructor be defined?
answer
- the = 0 pure-specifier
- final overrider still pure
- a body reachable by qualified name
- every derived destructor calls the base's
basics
~20 sA class is abstract while some pure virtual function, declared with = 0, is still its final overrider. A pure virtual may have a body, reached by qualified call, and a pure virtual destructor must be defined because derived destructors call it.
solid answer
~50 sWriting `= 0` (the **pure-specifier**) makes a virtual function **pure**, and a class is **abstract** while it has at least one pure virtual function whose final overrider is still pure. No object of an abstract class can exist except as a base subobject, so `Effect e;`, by-value parameters and returns, and `new Effect` are compile errors, while pointers and references are fine. A derived class that does not override every pure function stays abstract. A pure virtual function **may have a definition**, provided outside the class (a declaration cannot carry both `= 0` and a body); it is reached only by a qualified call such as `Effect::reset()`, which makes it a default that derived classes must opt into. A **pure virtual destructor** must be defined, because every derived destructor implicitly calls the base destructor; leaving it out is a link error.
code
cpp · 21 lines#include <cstdio>
struct Effect {
virtual ~Effect() = 0; // pure, yet must be defined
virtual void reset() = 0; // pure, with a default body below
};
Effect::~Effect() { std::puts("~Effect"); }
void Effect::reset() { std::puts("Effect::reset: clear shared state"); }
struct Delay : Effect {
void reset() override {
Effect::reset(); // qualified call: no virtual dispatch
std::puts("Delay::reset: clear the delay line");
}
};
int main() {
Delay d;
Effect& e = d;
e.reset();
}go deeper
Recall that = 0 makes a virtual function pure, a class with one is abstract and cannot be instantiated, and you use it through pointers or references.
Explain the final-overrider rule, why a derived class missing one override stays abstract, and how a pure function with a body is reached by qualified call.
Explain the pure virtual destructor: why it is used to make a class abstract, why it must be defined, and the link error you get when it is not.
Weigh what each pure function in a published base costs every implementer, and when a body-carrying pure function is a better default than a silently inherited one.
## Pure virtual functions and abstract classes A virtual function becomes **pure** when its declaration in the class ends with the **pure-specifier** `= 0`: ```cpp struct Effect { virtual ~Effect() = default; virtual void process(float* buf, int n) = 0; // pure virtual virtual void reset() = 0; // pure virtual }; ``` The standard defines a class as **abstract** if it has at least one pure virtual function for which the final overrider is still pure. Consequences: - no object of an abstract class can be created **except as a base class subobject** of a derived object, so `Effect e;`, `new Effect` and `Effect{}` are compile errors; - an abstract class cannot be the parameter or return type of a function that is defined or called, nor the type of an explicit conversion, because each would create an `Effect` object; - **pointers and references** to an abstract class are fine, and they are the whole point: `std::vector<std::unique_ptr<Effect>>`, `void route(Effect& fx)`; - an abstract class can still have **data members, constructors and non-virtual functions**; its constructors run when a derived object is built. ## Abstractness is inherited until every pure function is overridden ```cpp struct Delay : Effect { void process(float* buf, int n) override; // reset() not overridden: Delay is still abstract }; Delay d; // error: variable type 'Delay' is an abstract class ``` Abstractness follows the **final overrider**: `Delay`'s final overrider for `reset()` is still `Effect::reset`, which is pure. This is a frequent surprise after a base interface gains a new pure function: every derived class that has not implemented it stops compiling at the places that create it. The reverse is also allowed: an abstract class can derive from a concrete one, and a pure virtual function can override a virtual function that was not pure, forcing further derived classes to provide their own. ## A pure virtual function may have a body `= 0` does not mean "has no implementation". It means "the class is abstract and derived classes must override this". The standard notes that a single declaration cannot carry both the pure-specifier and a definition, so the body goes **outside the class**: ```cpp void Effect::reset() { /* clear state shared by all effects */ } struct Delay : Effect { void reset() override { Effect::reset(); // qualified call: no virtual dispatch /* clear the delay line */ } }; ``` A qualified call (`Effect::reset()`) suppresses the virtual mechanism and calls exactly that function. The pattern gives you a **default implementation that every derived class must explicitly opt into**, unlike a plain virtual default, which a derived class inherits silently. The standard requires a pure virtual function to be defined only if it is called with the qualified-id syntax, or as if with it, which is exactly what happens to destructors. ## Why a pure virtual destructor needs a definition Sometimes a base class should be abstract but has no natural pure function. Making the **destructor** pure does it: ```cpp struct EffectBase { virtual ~EffectBase() = 0; }; EffectBase::~EffectBase() = default; // still required ``` Destroying any derived object runs the derived destructor body and then the destructors of its bases, which the standard describes as called **as if by qualified name**, ignoring virtual overriding. So `Delay::~Delay` always calls `EffectBase::~EffectBase` directly. Omit the definition and the build fails at link time: with Apple clang 21 the linker printed `Undefined symbols for architecture arm64:` followed by `"Effect::~Effect()", referenced from:` and the `Delay::~Delay()` that needs it. ## Summary of the rules | Construct | Allowed? | |---|---| | `Effect e;` or `new Effect` with `Effect` abstract | no, compile error | | `Effect& r = delay;` or `Effect* p = &delay;` | yes | | a pure virtual function with an out-of-class body | yes; reached through `Effect::f()` | | `virtual void f() = 0 { }` inside the class | no, ill-formed | | a pure virtual destructor without a definition | link error once a derived class's destructor is needed | | a derived class missing one pure override | compiles until you create an object of it | Two neighbouring rules round this out: calling a pure virtual function through virtual dispatch from the abstract class's own constructor or destructor is undefined behaviour, and C++ has no `interface` keyword, so an abstract class with only pure virtual functions and a virtual destructor is the language's interface idiom.
- Why prefer a pure virtual function with a body over a plain virtual function with the same body?A plain virtual default is inherited silently, so a new derived class can forget to think about it. A pure virtual function with a body forces every concrete derived class to override it, while still letting each override reuse the shared part with a qualified call such as `Effect::reset()`. It turns an easy-to-miss default into a deliberate choice.
- Can an abstract class have a constructor, and who calls it?Yes. An abstract class can have data members and constructors to initialise them; only creating a complete object of it is forbidden. Its constructor runs as part of constructing a derived object, either called from the derived constructor's member initializer list or by default. Making that constructor `protected` documents that only derived classes may use it.
- A pure virtual function can never have an implementation.
- A derived class that overrides only some pure functions becomes concrete anyway.
- A pure virtual destructor needs no definition because it is pure.
- Abstract classes cannot have data members or constructors.
- You cannot hold a pointer or reference to an abstract class type.