I'm not a C++ guy, but I'm forced to think about this. Why is multiple inheritance possible in C++, but not in C#? (I know of the diamond problem, but that's not what I'm asking here). How does C++ resolve the ambiguity of identical method signatures inherited from multiple base classes? And why is the same design not incorporated into C#?
I thing (without having hard reference), that in Java they wanted to limit the expressiveness of the language to make the language easier to learn and because code using multiple inheritance is more often too complex for it's own good than not. And because full multiple inheritance is a lot more complicated to implement, so it simplified the virtual machine a lot too (multiple inheritance interacts especially badly with garbage collector, because it requires keeping pointers into the middle of object (at the beginning of the base))
And when designing C# I think they looked at Java, saw that full multiple inheritance indeed wasn't missed much and elected to keep things simple as well.
It does not. There is a syntax to call base class method from specific base explicitly, but there is no way to override only one of the virtual methods and if you don't override the method in the subclass, it's not possible to call it without specifying the base class.
There is nothing to incorporate.
Since Giorgio mentioned interface extension methods in comments, I'll explain what mixins are and how they are implemented in various languages.
Interfaces in Java and C# are limited to declaring methods only. But the methods have to be implemented in each class that inherits the interface. There is however large class of interfaces, where it would be useful to provide default implementations of some methods in terms of others. Common example is comparable (in pseudo-language):
Difference from full class is that this can't contain any data members. There are several options for implementing this. Obviously multiple inheritance is one. But multiple inheritance is rather complicated to implement. But it's not really needed here. Instead, many languages implement this by splitting the mixin in an interface, which is implemented by the class and a repository of method implementations, that are either injected into the class itself or an intermediate base class is generated and they are placed there. This is implemented in Ruby and D, will be implemented in Java 8 and can be implemented manually in C++ using the curiously recurring template pattern. The above, in CRTP form, looks like:
and is used like:
This does not require anything to be declared virtual as regular base class would, so if the interface is used in templates leaves useful optimization options open. Note, that in C++ this would probably still be inherited as second parent, but in languages that don't allow multiple inheritance it't inserted into the single inheritance chain, so it's more like
The compiler implementation may or may not avoid the virtual dispatch.
A different implementation was selected in C#. In C# the implementations are static methods of completely separate class and the method call syntax is appropriately interpreted by compiler if a method of given name does not exist, but an "extension method" is defined. This has the advantage that extension methods can be added to already compiled class and the disadvantage that such methods can't be overriden e.g. to provide optimized version.
|show 5 more comments|
The answer is that it doesn't work correctly in C++ in the event of namespace clashes. See this. To avoid namespace clashing you have to do all kinds of gyrations with pointers. I worked at MS on the Visual Studio team and I at least in part the reason they developed delegation was to avoid namespace collision altogether. Preiouvsly I had said that they also considered Interfaces to be part of the multiple inheritance solution, but I was mistaken. Interfaces are actuallly amazing and can be made to work in C++, FWIW.
Delegation specifically addresses namespace collision: You can delegate to 5 classes and all 5 of them will export their methods to your scope as first class members. On the outside looking in this IS multiple inheritance.
|show 4 more comments|