c++ - Why is Multiple Inheritance in Classes avoided? -


is because confusing , fields intermixed or other reason? allowed c++ not in java??

for example:

herbivore , carnivore derived animal , omnivore derived both herbivore , carnivore. won't fields mixed up.

why multiple inheritance in classes avoided? because confusing , fields intermixed or other reason?

multiple inheritance avoided because leads several problems can difficult fix novice programmer. possible problems:

  1. diamond inheritance.
  2. carelessly inheriting multiple interfaces (which weren't designed) can pollute object's "public" or "protected" section methods aren't useful particular object.
  3. you have aware of construction/destruction order when object inherits several other objects, otherwise might crashes due undefined behavior caused things double-delete. in other words, if object c inherits objects , b, , ~a somehow uses provided object b, ~a should called before ~b. i.e. in scenario class c: public b, public a{}: work, class c: public a, public b{}; crash when destroyed. bug can hard find.

but not in java??

ask java question that.

its allowed c++

multiple inheritance available in c++ because useful.

typical scenario - there couple of abstract interfaces class has support. let's "ireader", "iwriter" , "iuglyobject". have no common ancestor. support several interfaces without multiple inheritance, you'll either have make sure interfaces have common ancestor (which isn't possible) or you'll have define classes (derived interfaces want support), write lot of glue code forward calls class derived classes, typing. multiple inheritance can protected inherit interfaces , add few methods return pointer required interface.

class myclass: protected isomething, protected isomethingelse{ public:     isomething* getsomethinginterface(){ return this;}     isomethingelse* getsomethingekseinterface(){ return this;} protected: }; 

herbivore , carnivore derived animal , omnivore derived both herbivore , carnivore. won't fields mixed up.

there many ways design hierarchy of classes, , method used in example not perfect. could, example, abstract "eating behavior" class , store in "animal". allow me change animal behavior on fly , temporarily turn rabbits carnivores. or create virtual method either returns list of food types animal accepts (or tests if food acceptable animal), allow me make animel wants eat fried eggs , nothing else. there other ways.

class hierarchy doesn't have mimic real world, know...

when still learning c++, mi blow mind , result in bad things when experimenting.

if you're new, avoid now. multiple inheritance useful in scenario listed - class supporting multiple different interfaces without writing glue code. in other cases can avoided , isn't necessary.

if language has feature, doesn't mean have use feature. if language has feature bad reputation, doesn't mean should never use it. choose tools based on situation.


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -