Practice mode C++

In C++ multiple inheritance, how is the diamond problem, where a class inherits two copies of a common base, avoided?

Correct answer C. By using virtual inheritance for the common base class

Explanation

If B and C both derive from A and D derives from both B and C, D normally contains two separate A subobjects, causing ambiguity. Declaring class B : virtual public A and class C : virtual public A makes them share a single A subobject in D. The most-derived class is then responsible for constructing that virtual base.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.