Multiple inheritance problems with the compiler? - C ++

I read about multiple inheritance

What is the specific problem with multiple inheritance? http://en.wikipedia.org/wiki/Diamond_problem

http://en.wikipedia.org/wiki/Virtual_inheritance
http://en.wikipedia.org/wiki/Multiple_inheritance

But since the code does not compile until the ambiguity is resolved, does this multiple inheritance make it a problem only for compilers? - how does this problem affect me if I don't want to ever code the compiler

+4
source share
4 answers

If you want to write code that compiles, you need to know what problems can lead to its compilation and how to avoid such situations. It is your problem, as a compiler user, to create inheritance hierarchies so that they are compiled.

Also, if you don’t understand how multiple inheritance works, you may have incorrect assumptions about what your classes do. If classes behave differently than you expect, this will cause errors when you try to use them.

+4
source

The compiler writer will print a nasty error message and stop compiling your code if you have unresolved ambiguity due to multiple inheritance. When they say that the code will not compile until the ambiguity is resolved, you need to consider a few questions:

  • You do not have a work program until the uncertainty is resolved.
  • The compiler does not allow this for you.
  • Therefore, until you resolve it, this is your problem, not the compiler writer.
+1
source

No, this is not a problem for the compiler writer:

  • In general, a compiler writer can determine how multiple inheritance works.
  • In particular, for C ++, there are several solutions for the writer.

This is a problem for a C ++ programmer, but only if you don’t understand how MI works in C ++.

I have one general solution, which should be a base class that defines an open interface, which you then consider to have separate subkeys, which then execute as various abstract classes that are inherited by the inherited concrete leaf class:

------ | Base | ------ | | ------ ^ | ----------------- | | | ------ ------ ------ | A | | B | | C | ------ ------ ------ | | | | | | ------ ------ ------ ^ ^ ^ | | | ----------------- | ------- |Derived| ------- | | ------- 

Each of A, B, and C implements nonoverlapping subkeys of Base, which means that you can replace A for A 'for an alternative or improved implementation without affecting any other class.

+1
source

In general, yes, you hit a nail on the head. This greatly increases the complexity and effort associated with supporting the compiler, but for the programmer, it only adds a little extra complexity, mainly due to the need to be tiringly concrete if there is a problem with the diamond. (Which should be very rare in a well-designed object hierarchy.)

0
source

Source: https://habr.com/ru/post/1305297/


All Articles