0

I'm not a C++ guy, but I'm forced to think about this. Why is multiple inheritance possible in C++, when I'm not able to do it in C#? (I know the diamond problem, but that's not what I'm asking here). How does C++ distinguish the ambiguity of same method signatures inherited from multiple base classes? And why the same design can't be incorporated in C#?

Sandeep
  • 5,581
  • 10
  • 42
  • 62
  • It could be incorporated in C#; it was left out for the problems it brings (confusion, ambiguity,...) – Lorenzo Dematté Jan 29 '13 at 12:24
  • 3
    It is possible in C++ because Bjarne put it in the spec. It's not possible in C# because its designers did not put it in their spec (on purpose). If disambiguation is needed in C++ you do it as usual with the scope resolution operator `::`. – Jon Jan 29 '13 at 12:24
  • C++ "distinguish" the ambiguity by giving the programmer tools to resolve it themselves (virtual base classes, the `::` operator, for example). But they are usually cumbersome to use; that's why many other languages avoided the problem by using single inheritance between classes (in C#, you still have multiple interface implementation) – Lorenzo Dematté Jan 29 '13 at 12:26
  • @QuestionClosers : Why are you trying to close this question? Am I asking anything wrong here? – Sandeep Jan 29 '13 at 13:41
  • This type of question is likely to solicit debate and isn't a good fit for the *technical* Q&A format of StackOverflow. As such it's likely to be closed. However, it is the right subject-matter for Programmers.StackExchange.com - you should ask it there. – Paul Turner Jan 29 '13 at 13:54

4 Answers4

4

This is a question of choice. Anders Hejlsberg, the C# language designer, chose to leave multiple inheritance out of the language. You may wonder why... My guess would be that (1) multiple inheritance is often not needed, (2) multiple inheritance is often used in the wrong way (like so many object-orientation constructs) and (3) it would make the language and/or the compiler and/or static checking unnecessarily complex.

The CLR itself does not prevent multiple inheritance; hence, it is available in C++.NET.

Roy Dictus
  • 32,551
  • 8
  • 60
  • 76
3

You can't in Java or C# because it's a design decision built into the language. Whether you agree or not, the language designers decided that the difficulties of multiple inheritance of implementation, as done in C++, wasn't worth the cost.

C++ already allowed multiple inheritance of implementation when I was writing it in 1995.

That choice was made by Java back in 1995. C# followed suit later on for the same reasons.

I'll point out that both Java and C# allow you to implement as many interfaces as you want. It's multiple inheritance of implementation that's the issue.

I'll leave the answer as to how C++ disambiguates multiple inheritance of implementation to others who have used the language more recently than me.

duffymo
  • 305,152
  • 44
  • 369
  • 561
0

The ambiguity in the "diamond of death" is resolved using virtual inheritance. the wikipedia article is as good an explanation as any. Virtual Inheritance

It is also potentially resolved by just designing your class hierarchy better. As a rule of thumb, multiple inheritance is ok as long as you're inheriting multiple interfaces (in c++, pure abstract classes).

Grimm The Opiner
  • 1,778
  • 11
  • 29
  • I was about to post the same wiki link.The mitigation section explains beautifully how the multiple inheritance is supported in different language. – Amit Gupta Jan 29 '13 at 12:37
  • Feel free to give me an upvote. ;-) – Grimm The Opiner Jan 29 '13 at 12:41
  • This does not really answer the author's question. There are far better answers then yours. – Security Hound Jan 29 '13 at 12:44
  • I don't get it. Most of the time I use multiple inheritance I use it for composition, and thus inheriting purely abstract classes is pointless. – R. Martinho Fernandes Jan 29 '13 at 12:44
  • 1
    @Ramhound. "How is C++ overcoming the diamond issue?" "How does C++ distinguish the ambiguity of same method signatures inherited from multiple base classes?". Severe reading comprehension failure there on your part. – Grimm The Opiner Jan 29 '13 at 15:17
  • @R.MartinhoFernandes, using inheritance for polymorphism is an incredibly powerful feature, I'm surprised you don't find any use for it! Defaulting to using inheritance as composition runs contrary to another generally accepted "rule of thumb": see this [link](http://stackoverflow.com/questions/49002/prefer-composition-over-inheritance) – Grimm The Opiner Jan 29 '13 at 15:46
0

From msdn.micosoft link

Multiple-inheritance is supported in C++. However, just about all the other modern object-oriented languages, including Java, have chosen not to allow multiple-inheritance. (Some advanced languages, such as Eiffel, have attempted to work out the kinks of multiple inheritance)

The biggest problem with multiple-inheritance is that it allows for ambiguity when the compiler needs to find the correct implementation of a virtual method.

So, in the interest of keeping things simple, the creators of Java and C# decided not to allow multiple inheritance. However, there is an alternative to multiple inheritance, known as interfaces

So keeping all these issues in mind designers doesn't allow multiple-inheritance in language, but still language support it in some other way like interfaces. In addition of above answers & links you can also look to why doesn't c# support multiple inheritance

a.m.
  • 2,083
  • 1
  • 16
  • 22