0
0
CppConceptBeginner · 3 min read

What is vtable in C++: Explanation and Example

In C++, a vtable is a hidden table used by classes with virtual functions to support dynamic dispatch. It stores pointers to the virtual functions so the program can call the correct function at runtime based on the object's actual type.
⚙️

How It Works

Imagine you have a remote control that can operate different devices like a TV or a DVD player. The remote doesn't need to know exactly which device it controls; it just sends commands. Similarly, in C++, when you use virtual functions, the program needs a way to decide which function to call depending on the actual object type at runtime.

The vtable is like a menu or list inside each class that has virtual functions. This list holds addresses (pointers) to the actual functions that should run. When you call a virtual function through a pointer or reference, the program looks up the right function in the vtable and calls it. This process is called dynamic dispatch.

Each object of a class with virtual functions has a hidden pointer to its class's vtable. This setup allows the program to pick the correct function even if you only know the object through a base class pointer.

💻

Example

This example shows a base class with a virtual function and a derived class that overrides it. The program calls the function through a base class pointer, but the derived class's version runs because of the vtable.

cpp
#include <iostream>
using namespace std;

class Animal {
public:
    virtual void sound() {
        cout << "Animal makes a sound" << endl;
    }
};

class Dog : public Animal {
public:
    void sound() override {
        cout << "Dog barks" << endl;
    }
};

int main() {
    Animal* pet = new Dog();
    pet->sound();  // Calls Dog's sound() because of vtable
    delete pet;
    return 0;
}
Output
Dog barks
🎯

When to Use

Use vtable indirectly by declaring virtual functions in your classes when you want polymorphism. This means you want to write code that can work with different types of objects through a common interface and have the right function run automatically.

Common real-world uses include:

  • Designing flexible libraries where users can extend behavior by subclassing.
  • Implementing interfaces or abstract base classes.
  • Handling different types of objects in collections or algorithms without knowing their exact types.

The vtable mechanism is automatic in C++ when you use virtual functions, so you don't manage it directly but should understand it to write efficient and correct polymorphic code.

Key Points

  • vtable is a hidden table used for dynamic dispatch of virtual functions.
  • Each class with virtual functions has its own vtable.
  • Objects have a hidden pointer to their class's vtable.
  • Enables polymorphism by calling the correct function at runtime.
  • Programmers use virtual functions; the compiler creates and manages the vtable.

Key Takeaways

A vtable enables runtime selection of the correct virtual function in C++.
It is automatically created by the compiler for classes with virtual functions.
Objects store a hidden pointer to their class's vtable for dynamic dispatch.
Use virtual functions to achieve polymorphism and flexible code design.
Understanding vtable helps write efficient and correct object-oriented C++ code.