Q:

What is the Diamond problem? How can we get around it in c++?

0

What is the Diamond problem? How can we get around it?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer:

C++ allows multiple inheritances. Multiple inheritances allow a child class to inherit from more than one parent class. The diamond problem occurs when two superclasses of a class have a common base class. For example, in the following diagram, the “D class” gets two copies of all attributes of “A class”, which causes ambiguities. Let see the below image which shows what happens without virtual inheritance?

A A
| |
B C
\ /
D

The solution to this problem is the ‘virtual’ keyword. We make the classes “B” and “C” as virtual base classes to avoid two copies of class “A” in the “D” class.

A
/ \
B C
\ /
D

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Interview Questions For Experienced

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Why virtual functions cannot be static in C++?... >>
<< Friend class and function in C++?...