Explain the concept of vtable and vptr. How does the compiler resolve a virtual function call at runtime?
Last updated
The vtable is a per-class array of pointers to its virtual functions. The vptr is a hidden per-object pointer, set by the constructor, to its dynamic type's vtable.
A virtual call loads the object's vptr, indexes the function's fixed vtable slot, and calls that pointer (dynamic dispatch). Non-virtual calls are bound at compile time.
It is an implementation detail, not standard. During construction/destruction the vptr reflects the class currently being built, so virtual calls there don't reach derived overrides.