ProgrammingHardGATE CS · TCS NQT Prime · Product Company SDE

Why does adding a brand-new virtual function to a base class not increase the size of each object in that hierarchy (assuming objects already have at least one virtual function)?

  1. A. Every object already carries just one hidden vptr regardless of how many virtual functions exist; a new virtual function only adds one more entry to the shared class-wide vtable
  2. B. Virtual functions are always inlined away by the optimiser
  3. C. Virtual functions are stored inline inside every object
  4. D. The compiler removes the vptr whenever a new virtual function is added

Correct answer: A. Every object already carries just one hidden vptr regardless of how many virtual functions exist; a new virtual function only adds one more entry to the shared class-wide vtable

The vptr count per object stays at one no matter how many virtual functions a class declares; new entries are added to the one vtable shared by all instances of that class.

More OOP with C++ and Java questions

Exams that ask this topic