Difficulty: Intermediate
How do virtual functions work internally? Explain the vtable and vptr and what happens if you call a virtual function from a constructor.
Runtime polymorphism is the crown jewel of object-oriented C++, and interviewers love it because it lets them probe both language rules and how compilers implement them. Let's cover the what, then the how.
The what: when you call a method through a base class pointer or reference, a non-virtual function is resolved at compile time from the static type of the pointer. If you declare it virtual, the call is resolved at runtime from the object's dynamic type. So a Base* that actually points to a Dog calls Dog::speak. This is what lets you write a loop over a vector of Shape pointers and call area() without caring about the concrete shapes.
The how, in most compilers: each class with at least one virtual function gets a vtable, a static table of function pointers, one entry for each virtual function, created at compile time. Each object of such a class carries a hidden pointer called the vptr, set by the constructor to point to its class's vtable. A virtual call becomes two steps: load the vptr from the object, index into the vtable, and call through that pointer. Think of it as a restaurant menu: the object holds a pointer to its restaurant's menu, and asking for 'the special' looks it up in the menu at the time you order. This costs one extra indirection and prevents inlining, but the cost is small. The standard does not mandate vtables, but every major compiler uses them.
Consequences worth stating: objects of polymorphic classes are larger by one pointer (commonly 8 bytes on 64-bit systems); the vtable is per class, not per object; multiple inheritance can lead to several vptrs in one object; and a derived class's vtable entry is replaced by its override, while unoverridden entries point to the base implementation.
Now the classic trap: calling a virtual function inside a constructor or destructor. While Base's constructor runs, the object's dynamic type is Base, since the Derived part has not been built yet, and the vptr points to Base's vtable. So the call resolves to Base's version, not Derived's. The same applies in the destructor, because the derived part has already been destroyed. If that virtual function is pure virtual, the result is undefined behaviour or a linker or runtime error.
Also mention the override keyword (C++11), which makes the compiler verify that you are actually overriding a base virtual (catching signature mismatches like a missing const), and final, which prevents further overriding. Static functions cannot be virtual, and constructors cannot be virtual, since the vptr is not set up before construction. A virtual destructor is essential in base classes; we will cover it separately.
#include <iostream>
#include <memory>
#include <vector>
struct Animal {
Animal() { std::cout << "Animal ctor sees: "; whoAmI(); }
virtual ~Animal() = default;
virtual void speak() const { std::cout << "...\n"; }
virtual void whoAmI() const { std::cout << "Animal\n"; }
};
struct Dog : Animal {
void speak() const override { std::cout << "Woof\n"; }
void whoAmI() const override { std::cout << "Dog\n"; }
};
struct Cat : Animal {
void speak() const override { std::cout << "Meow\n"; }
};
int main() {
std::vector<std::unique_ptr<Animal>> zoo;
zoo.push_back(std::make_unique<Dog>());
zoo.push_back(std::make_unique<Cat>());
for (const auto& a : zoo) a->speak();
}
Even though a Dog is being built, the Animal constructor calls Animal::whoAmI because the derived part does not exist yet. After construction, speak() dispatches dynamically.
virtual functions, vtable, vptr, runtime polymorphism