How does a typical C++ compiler implement virtual function dispatch at run time?
Correct answer
B. Through a per-object vptr into the class's vtable
15 views
B. Through a per-object vptr into the class's vtable
15 views
Each polymorphic object carries a hidden vptr pointing at its class's vtable, an array of function pointers built at compile time, so a virtual call loads the slot and jumps indirectly. Overriding replaces the entry in the derived class's table, which is why the correct function is found without testing types. This indirection usually blocks inlining and explains the small performance cost often attributed to virtual calls.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is B? Ask here and get an explanation.