1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. How does a typical C++ compiler implement virtual function…

Added 30 Nov -0001

How does a typical C++ compiler implement virtual function dispatch at run time?

  1. Through a hash table keyed on the mangled function name
  2. Through a per-object vptr into the class's vtable Correct answer
  3. By scanning the inheritance tree on every call
  4. By storing the function body inside each object

Correct answer

B. Through a per-object vptr into the class's vtable

15 views

MCQSEXAM

Explanation

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.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is B? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C++ and Object-Oriented Programming MCQs

Practise all C++ and Object-Oriented Programming MCQs

Similar MCQs