If a class has any
virtual
functions that it, or another class, overrides, the class's first member is a pointer that precedes its other data members. This pointer (often called a
vptr
) references a table of
virtual
functions (often called a
vtbl
) associated with that class. Each class that inherits (and also the one that defines) any of those
virtual
functions will also have its own
vtbl
. The entries in a
vtbl
are function pointers that reference the appropriate implementation of each
virtual
function for that particular class. So what happens "under the hood" when you invoke
obj->func(args)
is
auto vtbl = obj->vptr;
vtbl[index of func](obj, args);
The index of
func
is hard-coded by the compiler, which also creates each
vtbl
, after which the linker gathers up all this stuff so that the loader can put it in memory when the program runs.