What is virtual inheritance in C++?
2022
What is virtual inheritance in C++?
- A.
C++ technique to enhance multiple inheritance.
- B.
C++ technique to ensure that a private member of base class can be accessed.
- C.
To avoid multiple inheritance of classes.
- D.
To avoid multiple copies of the base class in derived class.
Attempted by 235 students.
Show answer & explanation
Correct answer: D
Virtual inheritance ensures that only one shared instance of a common base class exists when using multiple inheritance—this prevents duplicate base-class subobjects in the diamond problem. Problem: If class B and class C both inherit from class A, and class D inherits from both B and C, then without virtual inheritance D will contain two separate A subobjects (diamond problem).