class D : public B1, public B2, where both B1 and B2 virtually inherit A. How many A subobjects does a D contain?
Correct answer
B. Exactly one, shared by both paths, because virtual bases are merged into one subobject
16 views
B. Exactly one, shared by both paths, because virtual bases are merged into one subobject
16 views
Virtual inheritance makes the base a shared subobject, so the diamond stores one A that both B1 and B2 refer to, which removes the ambiguity of d.A::member. Construction of the virtual base is the responsibility of the most-derived class, so D initialises A directly. The runtime cost is indirect access through a base offset table rather than a fixed displacement.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is B? Ask here and get an explanation.