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)?
- 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
- B. Virtual functions are always inlined away by the optimiser
- C. Virtual functions are stored inline inside every object
- 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
- Which java.util.function functional interface best fits a lambda that takes one argument of type T and returns a boolean?
- A base class method is declared final in Java. What does this guarantee about calls to it through a base-class reference, regardless of…
- cpp struct S { int a, b; S(int x) : b(x), a(b) { } }; int main() { S s(5); cout << s.a << " " << s.b; } Members a and b are declared in…
- What does List<? super Integer> allow, in contrast to List<? extends Integer>?
- java try { throw new ArithmeticException("x"); } catch (RuntimeException e) { System.out.print("R"); } catch (ArithmeticException e) {…
- Which OOP principle bundles data with the methods that act on it and hides the internal state behind an interface?