Runtime Polymorphism Terminology Reference

This reference provides an overview of runtime polymorphism terminology you'll encounter in this chapter. Use it as a quick lookup guide.

Polymorphism Types

Term Definition Example
Compile-time Polymorphism Resolved at compile time through function overloading and templates Function overloading, templates
Runtime Polymorphism Resolved at runtime through virtual functions Virtual function calls
Static Polymorphism Another term for compile-time polymorphism Templates, CRTP
Dynamic Polymorphism Another term for runtime polymorphism Virtual functions
Ad-hoc Polymorphism Function overloading and operator overloading Multiple print() functions
Parametric Polymorphism Templates allowing generic programming template<typename T>

Virtual Function Mechanics

Term Definition Example
Virtual Function Function declared with virtual keyword virtual void draw();
Pure Virtual Function Virtual function with no implementation virtual void draw() = 0;
Override Derived class providing implementation for virtual function void draw() override;
Final Preventing further overriding of virtual function void draw() final;
Virtual Destructor Destructor declared virtual for proper cleanup virtual ~Base();
Covariant Return Type Override returning pointer/reference to derived type Derived* clone() override;

Implementation Details

Term Definition Example
Virtual Table Table of function pointers for virtual functions vtable, one per polymorphic class
Virtual Pointer Hidden pointer in object pointing to vtable vptr, added by compiler
Virtual Function Call Indirect call through vtable ptr->virtualFunc();
Devirtualization Compiler optimization removing virtual call overhead When type known at compile time
Virtual Function Overhead Extra indirection cost of virtual calls One pointer dereference

Abstract Classes and Interfaces

Term Definition Example
Abstract Class Class with at least one pure virtual function Cannot be instantiated
Concrete Class Class with all virtual functions implemented Can be instantiated
Interface Class Abstract class with only pure virtual functions Defines contract
Implementation Class Concrete class implementing interface Provides actual behavior
Abstract Base Class Base class intended to be inherited from Often has pure virtual functions

Casting and Type Checking

Term Definition Example
dynamic_cast Runtime type-safe downcast for polymorphic types Derived* d = dynamic_cast<Derived*>(base);
static_cast Compile-time cast without runtime check Derived* d = static_cast<Derived*>(base);
const_cast Removes or adds const qualifier const_cast<int*>(constPtr);
reinterpret_cast Low-level reinterpretation of bits reinterpret_cast<char*>(&value);
typeid Runtime type information operator typeid(*ptr).name();
RTTI Runtime Type Information, enables dynamic_cast and typeid Enabled with virtual functions

Design Patterns

Term Definition Example
Strategy Pattern Encapsulating algorithms in separate classes Different sorting strategies
Template Method Base class defines algorithm, derived implements steps Hook methods in base class
Factory Pattern Creating objects through virtual function virtual Product* create();
Visitor Pattern Double dispatch using virtual functions Operations on object structure
Observer Pattern Notifying dependents of state changes Event handling system
Command Pattern Encapsulating request as object Undo/redo functionality

Object Slicing and References

Term Definition Example
Object Slicing Copying derived to base, losing derived parts Base b = derivedObj; copies only Base part
Reference Polymorphism Using references to achieve polymorphism void func(Base& obj);
Pointer Polymorphism Using pointers to achieve polymorphism Base* ptr = new Derived;
Pass by Reference Avoiding slicing by passing reference void process(const Shape& shape);
Container of Pointers Storing polymorphic objects via pointers std::vector<std::unique_ptr<Base>>

Virtual Destructor Requirements

Term Definition Example
Virtual Destructor Destructor marked virtual for proper cleanup virtual ~Base() { }
Proper Cleanup Calling correct destructor through base pointer Prevents resource leaks
Delete Through Base Pointer Deleting derived object via base pointer Requires virtual destructor
Non-virtual Destructor Undefined behavior deleting derived via base Without virtual destructor
Protected Destructor Preventing deletion through base pointer Alternative to virtual destructor

Performance Considerations

Term Definition Example
Virtual Call Overhead Indirect function call through vtable Small performance cost
Cache Locality vtable access may cause cache miss Performance consideration
Inlining Prevention Virtual functions typically can't be inlined Unless devirtualized
Vtable Size Memory overhead per polymorphic class One vtable per class
Vptr Size Memory overhead per polymorphic object Typically one pointer

Advanced Concepts

Term Definition Example
Multiple Dispatch Resolution based on multiple object types Visitor pattern approximation
Single Dispatch Resolution based on single object type Standard C++ virtual functions
Virtual Inheritance Sharing base class in diamond hierarchy class Derived : virtual public Base
Virtual Table Layout Organization of virtual function pointers Compiler-specific
Thunk Small piece of code adjusting this pointer Used in multiple inheritance