Are there any scenarios in which multiple inheritence is necessary? Please provide examples.
When the developer is lazy and doesn't want to produce a better class design that wouldn't need multiple inheritance ;-) I primarily code in C# and have never found a scenario that caused me to curse their design of only allowing single inheritance.
That being said, being able to implement multiple interfaces is really handy.
If you want to simulate something like Interfaces that other OOP languages have, then you'll need to use multiple inheritence.
With pure abstract classes you can have interfaces, like you do in Java or C#
There are no scenarios where anything more than a Turing machine is necessary.
Anything which "requires" inheritance, and which you want to use more than once on a single class, or combine in a single class, "requires" multiple inheritance.
Common C++ idioms which use inheritance include:
- Java-style interfaces using abstract classes containing only pure virtual functions
- Mixins, including simulated dynamic binding via CRTP (http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern), or
boost::noncopyable
. - Policy-based design (http://en.wikipedia.org/wiki/Policy-based_design)
- Provision of type traits
So, if you want a single class implementing multiple interfaces then you need multiple inheritance. If you want a policy-based iterator type, then you need multiple inheritance (once for the policy, and once for the traits).
I put "requires" in snigger quotes because obviously in all cases you can write a program having the same output, and which does not use multiple inheritance (the "Turing complete is all we care about" argument). However, Turing completeness is not all we care about, and we usually have goals in writing software beyond Turing's concept of a program with input and output. We care what the source looks like. Hence, "necessary".