Base's constructor calls a virtual function that Derived overrides, and a Derived object is being constructed. Which version runs?
A
The program fails to compile, because virtual calls in constructors are illegal and are always rejected by every conforming compiler
B
Derived's version, because the dynamic type of the object is already Derived from the moment the allocation completes
C
Base's version, because during base construction the vptr still refers to Base's vtable and the Derived part does not yet exist
Correct answer
D
It is undefined behaviour, so either version may run depending on the compiler and on the optimisation level used
Correct answer
C. Base's version, because during base construction the vptr still refers to Base's vtable and the Derived part does not yet exist
18 views
⚔️
Challenge a friend on C++ and Object-Oriented Programming
Same MCQs, timed, live leaderboard. Send the link on WhatsApp – no sign-up needed.
Create challenge
Explanation
Virtual dispatch during construction is limited to the class whose constructor is running, since the derived members are not yet initialised. The same restriction applies in destructors, where the dynamic type reverts as each level is torn down. Calling a pure virtual function from a constructor is worse: it is undefined behaviour and usually aborts with a pure virtual call error.
Reference
Asked in: NTS, PPSC IT, Campus Interviews
Doubts & discussionNot sure why the answer is C? Ask here and get an explanation.
Ask a doubt about this MCQ
‹ Previous MCQ Why is the override specifier preferred on a derived virtual function? Next MCQ › Which statement about std::unique_ptr is correct?