C++ supports multiple inheritance. What is the “diamond problem” that can occur with multiple inheritance? Give an example.?
Ref: https://edurev.in/question/504479/C-supports-multiple-inheritance-What-is-the-“diamond-problem”-that-can-occur-with-multiple-inherita
“Diamond problem” refers to a class inheritance graph where a class derives from two different bases, which, in turn, derive from the same base.
To give a concrete example, consider std::fstream
It was a problem for the inventors of early OO programming languages because it wasn’t clear how to avoid two copies of the shared base (in this case, std::basic_ios), and even if the shared bases are combined in the single subobject, how to avoid double-initializing it. There were also complications for early garbage collection algorithms (reportedly, that’s why Simula was limited to single inheritance)
Even in C++, which solved this problem early on by marking every base you don’t want to duplicate as virtual (so it’s class std::basic_istream : public virtual std::basic_ios) and by tweaking order of initialization/destruction to pass over virtual bases only once, there is a trace of that problem remaining: implicit copy- and move-assignment may or may not double-assign the virtual base subobjects, depending on the implementation (gcc will double-assign, and will give you a warning if it could be a problem)
|
Explore Courses for JEE exam
|