Pure virtual
A pure virtual function is a virtual function declared with = 0. The class that contains it is abstract: you cannot construct an object of that type. A derived class must override the function, or it stays abstract too.
Detailed Explanation
You want a Shape that promises area() without knowing how to compute it. A normal virtual can have a default body. A pure virtual cannot be called through that type unless you also provide a body (rare) and call it with qualification. The = 0 is the contract: descendants supply the implementation.
Syntax: virtual double area() const = 0;. The = 0 is not an assignment and not a default argument. It is the pure-specifier. You may still write a definition later: double Shape::area() const { return 0; }. Derived classes can call Shape::area() explicitly. The class remains abstract. The usual reason for a body is a pure virtual destructor: the destructor will run during derived destruction and must have a definition.
A class with at least one pure virtual is an abstract class. Shape s; does not compile. Shape* and Shape& are fine; they refer to a derived object that completed the contract. new Shape does not compile.
If Circle overrides area but forgets draw, and draw is also pure, Circle is still abstract. The compiler error for Circle c; will mention the missing override. override on the derived function documents the intent and catches signature drift.
Pure virtuals are how C++ writes interfaces. A class of only public pure virtuals plus a virtual destructor is the analogue of a Java interface. Multiple inheritance of several such classes is the usual way to mix interfaces. There is no separate interface keyword.
You can inherit a pure virtual and leave it pure, redeclare it = 0, or implement it. Redeclaring = 0 in the middle of a hierarchy is a way to force further descendants to reimplement a function you do not want to inherit as-is.
A pure virtual call that actually happens (you constructed a derived that somehow still has a pure slot, or you call one from a constructor before the override is installed) is undefined behaviour. The practical version of that bug is calling a pure virtual from a base constructor: the derived override is not yet active, and the vtable slot may point at a __cxa_pure_virtual trap.
Prefer a pure virtual over a virtual that throws "not implemented" or returns a dummy. The dummy compiles and fails in production. The pure virtual fails at compile time when someone tries to create the incomplete type.
Code Examples
#include <iostream>
#include <memory>
#include <vector>
struct Shape {
virtual ~Shape() = default;
virtual double area() const = 0;
};
struct Circle : Shape {
double r = 2.0;
double area() const override { return 3.14159 * r * r; }
};
int main() {
// Shape s; // error: abstract
std::vector<std::unique_ptr<Shape>> scene;
scene.push_back(std::make_unique<Circle>());
std::cout << scene[0]->area() << '\n'; // prints: 12.5664
}struct Plugin {
virtual ~Plugin() = 0; // still needs a definition
virtual void start() = 0;
};
inline Plugin::~Plugin() = default; // required: derived dtors call thisCommon Gotchas
Forgetting the virtual destructor on an interface
`delete` through `Shape*` without `virtual ~Shape()` skips the derived destructor. Always give an interface a public virtual destructor, or a protected non-virtual one if you never delete through the interface.
A pure virtual destructor without a body
linking fails with an undefined `~Plugin`. Provide `= default` in a `.cpp` or as an inline definition.
Calling a pure virtual from a constructor
the derived override is not installed. The program aborts or worse. Use a two-phase `start()` after construction.
`= 0` can sit next to a default argument
`virtual void log(int n = 0) = 0;` is legal. The default still binds statically.