liveBook · Manning

Pure virtual member function

As explained in 100 C++ Mistakes and How to Avoid Them

A pure virtual function is a C++ virtual member function declared without a required implementation in the declaring class and intended to be overridden by derived classes.

A pure virtual member function declares behavior without providing an implementation in the declaring class. Derived concrete classes must provide the function body. This creates an interface-like base that can relate otherwise unrelated classes, but only when they share meaningful semantics rather than merely similar names or methods. section 7.7

Mistake: inheriting from appearance

Making Horse inherit from Square because both have move creates misleading relationships and invites casts such as dynamic_cast<Horse*>. A safer design separates the classes, then introduces a small Moveable interface when both genuinely represent movable objects.

How 100 C++ Mistakes and How to Avoid Them covers it

How deep it goes
Defines the interface pattern and shows implementations, but stops short of detailed object-model or language-runtime internals.
Examples
Uses Square, Horse, Moveable, dynamic_cast, and a collection of Moveable pointers to contrast misleading and meaningful relationships.
What it assumes
Assumes basic familiarity with C++ classes, inheritance, and derived classes while teaching interface-like behavior through examples.
What you can do after
Apply it via an interface-like base when classes share meaningful behavior, while avoiding inheritance based only on matching method names.
How it connects
Connects pure virtual functions with inheritance, abstract classes, interfaces, polymorphism, and semantic class relationships.

Also explained in

Compare how 2 books explain pure virtual member function →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 C++ Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.