Each object (that belongs to a class with at least one virtual function) has a pointer, called a vptr
. It points to the vtbl
of its actual class (which each class with virtual functions has at least one of; possibly more than one for some multiple-inheritance scenarios).
The vtbl
contains a bunch of pointers, one for each virtual function. So at runtime, the code just uses the object’s vptr
to locate the vtbl
, and from there the address of the actual overridden function.
In your specific case, Polygon
, Rectangle
, and Triangle
each has a vtbl
, each with one entry pointing to its relevant area
method. Your ppoly1
will have a vptr
pointing to Rectangle
‘s vtbl
, and ppoly2
similarly with Triangle
‘s vtbl
. Hope this helps!