Why do we need virtual functions in C++?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Virtual functions enable runtime polymorphism in C++, allowing code to call behavior through base-class interfaces while executing derived-class implementations. This is fundamental for extensible designs where concrete types vary at runtime. Without virtual dispatch, inheritance is less useful for substitutable behavior.
Core Sections
Static Dispatch Versus Dynamic Dispatch
By default, non-virtual member function calls are resolved at compile time based on static type. Virtual functions are resolved at runtime based on actual object type when accessed through base references or pointers.
This difference is why virtual functions are necessary for polymorphic APIs.
Designing Interfaces with Virtual Functions
Virtual methods allow callers to depend on abstractions rather than concrete implementations.
Client code can store heterogeneous objects in one container of Shape pointers and call area uniformly.
Why Virtual Destructors Matter
If a class has any virtual function and will be deleted through base pointers, its destructor should be virtual. Otherwise derived cleanup may not execute.
This rule prevents leaks and undefined behavior in polymorphic ownership patterns.
Template Method and Extensibility
Virtual hooks enable reusable base workflows with specialized extension points.
This pattern keeps shared lifecycle logic centralized while allowing behavior variation.
Performance and Design Tradeoffs
Virtual calls add indirection and can limit some compile-time optimizations. In most business software, this cost is negligible compared with design clarity and extensibility gains. Use virtual dispatch where substitutability is required, and prefer non-virtual functions for stable utility logic.
Marking overrides with override and occasionally final improves correctness and communicates intent.
Virtual interfaces also improve testability. You can inject fake implementations in unit tests without changing production callers, which reduces coupling and speeds feedback loops. This is especially useful in systems that integrate external services, file systems, or network dependencies.
When Not to Use Virtual Functions
If behavior never varies by subtype, virtual functions add unnecessary complexity. Alternative mechanisms include templates, composition, and function objects. Choose virtual dispatch specifically for runtime variability across a shared interface.
Common Pitfalls
- Forgetting virtual destructors in polymorphic base classes.
- Omitting
overrideand silently creating new methods instead of overriding. - Overusing virtual methods where composition or templates are simpler.
- Calling virtual functions from base constructors and expecting derived behavior.
- Exposing too many virtual hooks and making class contracts hard to reason about.
Summary
- Virtual functions provide runtime polymorphism through base interfaces.
- They enable extensible designs where implementations vary dynamically.
- Virtual destructors are required for safe polymorphic deletion.
overrideand focused interface design improve maintainability.- Use virtual dispatch intentionally when runtime substitutability is needed.
Related reading
- Why does boostequals require ranges to be copyable?
- Why does C++ code for testing the Collatz conjecture run faster than hand-written assembly?
- Why does stdcopy_n not increment the Input iterator n times?
- Why does stderase_if with a mutable predicate function have different behavior for older compilers?
- Why does stdnth_element return sorted vectors for input vectors with N 33 elements?
- Why does stdsleep_forstdchronohoursmax return immediately on linux?
- Why doesn't Boost.Range is_sorted require forward iterators?
- Why Eigen is 5x slower than ublas on following example?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.