Correct option is B
The diamond problem in C++ is resolved using
virtual inheritance, which ensures that the derived class receives a single instance of the base class, avoiding ambiguity caused by multiple inheritance paths.
Important Key Points:
1. The
diamond problem arises when a class inherits from two classes that share a common base class, leading to ambiguity in resolving which base class method or property to access.
2.
Virtual inheritance eliminates the duplication of the base class, ensuring only a single instance is inherited by the derived class.
3. Syntax for virtual inheritance:
Knowledge Booster:
·
Function overloading: Resolves ambiguity between functions with the same name but different parameters, not the diamond problem.
·
Single inheritance: Avoids the diamond problem but limits inheritance to a single parent class, which is less flexible.
·
Templates: Used for generic programming but do not address the diamond problem.
·
Avoiding constructors: Irrelevant, as constructors cannot resolve inheritance ambiguities.
