Virtual functions in C++ enable dynamic dispatch. When you call a member function through a base pointer or a reference, the version that runs is chosen at run time from the actual type of the object, and not from the static type of the expression. That single mechanism turns a family of related classes into something you can handle generically, and it is the foundation of every plugin system, every GUI framework and the strategy pattern in C++.
The previous lesson in this module covered "How Does Inheritance Reuse a Base Class in C++?", and the next lesson in this module is about "What Are Abstract Classes and Pure Virtual Functions?".
Static binding versus dynamic binding
Without the virtual keyword, the call is resolved at compile time from the type of the pointer or the reference.
#include <iostream>
#include <string>
class Shape {
public:
explicit Shape(const std::string& name) : name_(name) {}
double area() const { return 0.0; }
const std::string& name() const { return name_; }
private:
std::string name_;
};
class Circle : public Shape {
public:
Circle(const std::string& name, double radius)
: Shape(name), radius_(radius) {}
double area() const { return 3.14159 * radius_ * radius_; }
private:
double radius_;
};
int main() {
Circle disc("disc", 2.0);
Shape& as_shape = disc;
std::cout << "through base: " << as_shape.area() << '\n';
std::cout << "direct call: " << disc.area() << '\n';
return 0;
}The output shows the problem. The base version ran even though the object is a circle.
through base: 0
direct call: 12.5664And that is static binding. The compiler saw a Shape reference and it called Shape::area.
Adding virtual
Marking the base function as virtual changes the rule. The compiler builds a table of the virtual functions for each class, and it looks up the right one at run time.
#include <iostream>
#include <string>
class Shape {
public:
explicit Shape(const std::string& name) : name_(name) {}
virtual double area() const { return 0.0; }
const std::string& name() const { return name_; }
private:
std::string name_;
};
class Circle : public Shape {
public:
Circle(const std::string& name, double radius)
: Shape(name), radius_(radius) {}
double area() const override { return 3.14159 * radius_ * radius_; }
private:
double radius_;
};
int main() {
Circle disc("disc", 2.0);
Shape& as_shape = disc;
std::cout << "through base: " << as_shape.area() << '\n';
std::cout << "direct call: " << disc.area() << '\n';
return 0;
}Now the derived version runs in both of the cases.
through base: 12.5664
direct call: 12.5664And the override keyword is not required by the language, but it is worth always writing it. If the signature does not match the base exactly, then the compiler rejects the program instead of silently creating a second and unrelated function.
How the dispatch works
The compiler adds a hidden pointer to every class that has virtual functions. That pointer points at a vtable, which is an array of function pointers with one slot per virtual function in the class. Calling a virtual function through a base pointer therefore becomes three steps. You read the object's vtable pointer, you read the entry for that function, and you call it.
The cost is one extra indirection, which is why the virtual calls are slightly slower than the direct ones, and why the compiler can devirtualise a call when it can prove the dynamic type.
#include <iostream>
#include <memory>
#include <string>
#include <vector>
class Shape {
public:
virtual ~Shape() = default;
virtual double area() const = 0;
virtual const char* kind() const = 0;
};
class Rectangle : public Shape {
public:
Rectangle(double width, double height) : width_(width), height_(height) {}
double area() const override { return width_ * height_; }
const char* kind() const override { return "rectangle"; }
private:
double width_;
double height_;
};
class Circle : public Shape {
public:
explicit Circle(double radius) : radius_(radius) {}
double area() const override { return 3.14159 * radius_ * radius_; }
const char* kind() const override { return "circle"; }
private:
double radius_;
};
int main() {
std::vector<std::unique_ptr<Shape>> shapes;
shapes.push_back(std::make_unique<Rectangle>(3.0, 4.0));
shapes.push_back(std::make_unique<Circle>(2.0));
double total = 0.0;
for (const auto& shape : shapes) {
std::cout << shape->kind() << ": " << shape->area() << '\n';
total += shape->area();
}
std::cout << "total: " << total << '\n';
return 0;
}The output shows a single loop handling two different types.
rectangle: 12
circle: 12.5664
total: 24.5664And that is the payoff of dynamic dispatch. The loop knows nothing about rectangles or circles. It only knows the interface.
The rules of overriding
- A virtual function is overridden by a derived function with the same name, the same parameter types and the same return type. A different parameter list is an overload and not an override.
- The access level of the base does not matter. A public base function can be overridden by a private derived one.
- The override can be const only if the base is const.
- A static member function cannot be virtual.
- A member function that calls another virtual function dispatches on the dynamic type of the object, so the call inside the base reaches the derived override.
#include <iostream>
class Shape {
public:
virtual ~Shape() = default;
double total_area() const {
return area() * 2.0; // dispatches to the derived area()
}
virtual double area() const { return 0.0; }
};
class Square : public Shape {
public:
explicit Square(double side) : side_(side) {}
double area() const override { return side_ * side_; }
private:
double side_;
};
int main() {
Square square(3.0);
std::cout << "area: " << square.area() << '\n';
std::cout << "total: " << square.total_area() << '\n';
return 0;
}The output shows the base's helper reaching the derived implementation.
area: 9
total: 18Pure virtual functions and abstract classes
A virtual function can be declared with = 0, and that makes it pure virtual. A class with a pure virtual function cannot be instantiated. It is abstract, and it exists only as a base.
#include <iostream>
#include <string>
class Shape {
public:
virtual ~Shape() = default;
virtual double area() const = 0;
virtual std::string describe() const {
return "shape with area " + std::to_string(area());
}
};
class Triangle : public Shape {
public:
Triangle(double base, double height) : base_(base), height_(height) {}
double area() const override { return base_ * height_ / 2.0; }
private:
double base_;
double height_;
};
int main() {
Triangle triangle(6.0, 4.0);
std::cout << triangle.describe() << '\n';
return 0;
}The output shows the base's non-virtual helper using the derived implementation of the pure virtual one.
shape with area 12.000000Quick reference
- Without virtual the call is resolved from the static type at compile time, and with virtual it is resolved from the dynamic type at run time.
- An override needs the same name, the same parameter types and the same return type, and the
overridekeyword makes the compiler check it. - The vtable is an array of function pointers for each class, and it is reached through a hidden pointer in every object.
- A pure virtual function, which is written as
= 0, makes the class abstract. - The line
virtual ~Shape() = default;in the base is what makes the polymorphic deletion safe.
Try it yourself: write a Shape base with a non-virtual area method and two derived classes. Call the area through base references and note that the base version runs. Then add the virtual and the override keywords and call it the same way. Finally, store the shapes in a vector of base pointers and sum their areas with a single loop.