views:

96

answers:

2

Possible Duplicate:
A use for multiple inheritance ?

How is multiple inheritance really a useful construct in C++. Why use it? What are examples of where it can solve problems that can not be solved in some other clean way? What are some examples of how to use it in a useful way? How is it misused? What are the problems with it? Why do people not like it?

A: 

There's nothing wrong with MI, just with C++'s implementation. If you inherit just the interface or just the implementation of one thing (privately) you don't get into any trouble. If you do both in the same base class, trouble is very difficult to avoid.

Charles Eli Cheese
A: 

Well, inheritance specifies an "is a" relationship, so you'd use multiple inheritance when a class "is" multiple different things. For example, a deque is both "BackInsertable" and "FrontInsertable". BackInsertable could force a requirement for a push_back method and likewise for FrontInsertable and push_front. Without multiple inheritance, you'd have to have super classes for BackInsertable, FrontInsertable, and BackAndFrontInsertable. Obviously this doesn't scale well when you add more complexity.

The problems with multiple inheritance arise when two super classes define the same member variables, or when you run into the diamond problem (D1 and D2 inherit B, and D3 inherits D1 and D2, inheriting B twice).

People don't like it because it's very easy to run into these problems accidentally. As Antiguru said though, it's not so much that people dislike multiple inheritance, they just dislike C++'s implementation of it (and of course, you have people who just loathe object oriented programming altogether, and to them, multiple inheritance is pure evil).

Peter Alexander