C++
C++ virtual functions and the vtable: what happens behind the scenes?
When you write virtual, the compiler quietly adds a hidden pointer and a function table to your class. Here's how polymorphism works, why virtual destructors matter, and what a vtable looks like in compiled code.
Polymorphism is usually taught with animals: Animal* a = new Cat(); a->speak(); and the screen prints "Meow". It's a nice example, but the real question goes unanswered: how does the running program know this pointer is actually a cat? When I started doing reverse engineering, I saw the answer inside compiled code: the vtable.
A quick reminder
struct Shape {
virtual double area() const = 0;
virtual ~Shape() = default;
};
struct Circle : Shape {
double r;
explicit Circle(double r) : r(r) {}
double area() const override { return 3.14159 * r * r; }
};
double total(const std::vector<std::unique_ptr<Shape>>& shapes) {
double t = 0;
for (const auto& s : shapes) t += s->area(); // which area()? decided at runtime
return t;
}Without virtual, the compiler would bind s->area() to Shape::area at compile time. With virtual, the decision is left to runtime.
The vtable and the vptr
Most compilers implement this as follows (the standard doesn't require it, but in practice everyone does it this way):
- For every class with at least one virtual function, a vtable is created: a constant table holding the addresses of that class's virtual functions.
- Every object of that class gets a hidden pointer at its start: the vptr. It points to the vtable of the object's actual class.
- The call
s->area()really becomes: read the object's vptr, take the relevant entry from the table, jump to that address.
You can see this yourself: an otherwise empty class with a virtual function usually has a sizeof of 8 on a 64-bit system. Those 8 bytes are the vptr.
The vtable in compiled code
On x86-64, a virtual call looks roughly like this:
mov rax, qword ptr [rdi] ; the object's first 8 bytes: the vptr
call qword ptr [rax + 0x10] ; the function in the vtable's third slotRecognizing this pattern is very valuable in reverse engineering. If you see Ghidra writing a constant address into an object's first field, it's most likely a constructor setting the vptr, and the table at that address is the list of the class's virtual functions. It's one of the strongest clues for recovering a program's class hierarchy without ever seeing the source. I mentioned similar patterns in my post on why assembly is still worth knowing.
Why virtual destructors matter
If you delete an object through a base-class pointer, the base class destructor must be virtual. Otherwise only the base part's destructor runs and the derived class's resources leak; according to the standard, it's undefined behavior.
std::unique_ptr<Shape> s = std::make_unique<Circle>(2.0);
// if Shape's destructor weren't virtual, Circle's destructor wouldn't be calledThe rule is simple: if your class has a virtual function, make the destructor virtual too.
override and final
Writing override isn't mandatory, but there's a very good reason to: if you get the signature wrong (forgetting a const, say), the compiler gives an error. Without it, you silently define a new function and polymorphism doesn't work. final says a class or function can't be overridden further; with that information, the compiler can sometimes turn a virtual call into a direct one.
Does it cost anything?
A virtual call has one extra indirection compared to a normal call, and it often prevents the compiler from inlining the function. In most programs you won't notice. In hot loops called millions of times per second it can matter; there, compile-time alternatives like templates or std::variant may be preferable.
When I first learned virtual functions, I saw them as a language feature. When I first found a vtable in compiled code, I understood they were actually a very simple idea: a table and a pointer. Looking beneath abstractions is the shortest path to using them well.