C++ Tutorial

Polymorphism

Virtual functions, pure virtual functions, and VTables

What is Polymorphism in C++?

Polymorphism (meaning "many forms") is the ability of an object to behave differently depending on its actual type, even when accessed through a general pointer or reference. In C++, this is achieved through Virtual Functions.
cpp
class Animal {
public:
    // virtual: Ready to be overridden!
    virtual void speak() { std::cout << "..." << std::endl; }
};

class Dog : public Animal {
public:
    void speak() override { std::cout << "Bark!" << std::endl; }
};

Animal* a = new Dog(); // A Dog treated as an Animal!
a->speak(); // Output: "Bark!" (NOT "...")

Comparisons: Compile-time vs Run-time Polymorphism

Wait! C++ has two types of polymorphism:
  1. Compile-time (Static): Function overloading and Templates. This is much faster but less flexible.
  2. Run-time (Dynamic): Inheritance and Virtual Functions. This is slightly slower but allows for powerful plugin-style systems.

Abstract Classes & Pure Virtual Functions

Sometimes your base class is just a placeholder and shouldn't actually exist on its own. A Pure Virtual Function is one that has no implementation in the base class.
cpp
class Shape {
public:
    // Pure Virtual: Subclasses MUST implement this!
    virtual void draw() = 0; 
};

// Shape s; // ERROR: Cannot create an object of an abstract class!

The "VTable" (How it Works Under the Hood)

C++ achieves dynamic polymorphism using a Virtual Method Table (VTable). For every class with virtual functions, the compiler creates a table of function pointers. Each object stores a hidden "vptr" pointing to its class's VTable.
TaskActionResult
Object CallFollow vptrJumps to the correct function address.
Performance+1 Pointer DereferenceSlightly slower than a normal function call.

Important: Virtual Destructors!

If your base class has any virtual functions, it MUST have a virtual destructor. Otherwise, when you delete a child through a parent pointer, the child's destructor won't be called, causing a memory leak!
cpp
class Base {
public:
    virtual ~Base() {} // Essential for safety!
};

Summary

TermDescription
virtualMethod that can be overridden at runtime
overrideExplicitly tell the compiler it's an override
finalPrevent a class/method from being overridden
= 0Pure virtual; makes class "Abstract"
Base ClassThe parent providing the vtable entry
Best ForPlugin architectures, generic collections, UI systems
VTableThe hidden function lookup table
vptrThe hidden pointer inside every object
CostOne extra pointer lookup per call
SizeObjects with virtual functions are 1 pointer larger!
C++20Added the constexpr virtual feature!
StaticCannot use virtual for static methods!
BindingLate-binding (Run-time) vs Early-binding (Compile-time!)
ReferenceWorks exactly like pointers for polymorphism
TemplatesThe alternative to virtual functions for high performance
Safe DeleteAlways use a virtual destructor in the base class!