Polymorphism in C++: Compile-Time and Runtime Behavior
What is Polymorphism in C++: Compile-Time and Runtime Behavior? Compile-time polymorphism resolves function calls at build time with zero runtime overhead using Function Overloading, Operator Overloading, and Templates. Runt...
What is the difference between compile-time (static) polymorphism and runtime (dynamic) polymorphism in C++?
Explanation & In-Depth Details
```text
┌──────────────────────┬───────────────────────────────┬───────────────────────────────┐
│ Feature │ Static / Compile-Time │ Dynamic / Runtime │
├──────────────────────┼───────────────────────────────┼───────────────────────────────┤
│ Mechanism │ Overloading & Templates │ Virtual Functions & VTables │
│ Dispatch Timing │ Compiler resolves at compile │ CPU resolves at runtime │
│ Performance Overhead │ Zero runtime cost (Inlineable)│ VTable pointer lookup indirection│
│ Memory Overhead │ Slight code bloat (templates) │ 8-byte VPTR per object + VTable│
│ Flexibility │ Strict type checking at build │ Dynamic plug-and-play plugins │
└──────────────────────┴───────────────────────────────┴───────────────────────────────┘
```
2. How Runtime Dispatch Works:
Each polymorphic class has a Virtual Method Table (`vtable`) containing function pointers. Each object stores a virtual table pointer (`vptr`).
Code Example
#include <iostream>
#include <vector>
#include <memory>
// 1. Compile-time Polymorphism (Function Overload)
void render(int score) { std::cout << "[Static] Integer score: " << score << "\n"; }
void render(double health) { std::cout << "[Static] Float health: " << health << "%\n"; }
// 2. Runtime Polymorphism (Virtual Hierarchy)
class Shape {
public:
virtual ~Shape() = default;
virtual void draw() const = 0; // Dynamic dispatch target
};
class Circle : public Shape {
public:
void draw() const override { std::cout << "[Dynamic] Drawing Circle (o)\n"; }
};
class Square : public Shape {
public:
void draw() const override { std::cout << "[Dynamic] Drawing Square [ ]\n"; }
};
int main() {
// Compile-time resolution
render(100);
render(98.5);
// Runtime dynamic dispatch
std::vector<std::unique_ptr<Shape>> canvas;
canvas.push_back(std::make_unique<Circle>());
canvas.push_back(std::make_unique<Square>());
for (const auto& s : canvas) {
s->draw(); // Resolved via vptr -> vtable at runtime
}
return 0;
}Interview Question & Tips
A: A vtable (virtual method table) is an array of function pointers created by the compiler for every class with virtual functions. It usually resides in the read-only data segment (`.rodata`).
Q: What is the memory overhead of virtual functions in an object?
A: Each object contains an extra hidden pointer (`vptr`, usually 8 bytes on 64-bit systems) pointing to its class vtable.
Best Practices
• Use runtime polymorphism when collections of heterogeneous types must be managed uniformly.
• Always mark overriding methods with the `override` keyword to catch signature mismatches at compile time.
This study reference for Polymorphism in C++: Compile-Time and Runtime Behavior is curated and verified against recognized curriculum benchmarks, peer-reviewed textbooks, and official technical documentation: