Multiple inheritance
Moderator: Coders of Rage
Multiple inheritance
So I was working on a C#/XNA yesterday and found that you couldn't inherit from multiple classes and through this long and boring adventure that I sharn't go into details about I found a lot of people off forums saying in C++ this is a poor design.
Whats your opinions on this? I personally think its like friend classes, very handy if used correctly but also a poor design when its out of pure lazyness.
Whats your opinions on this? I personally think its like friend classes, very handy if used correctly but also a poor design when its out of pure lazyness.
- Ginto8
- ES Beta Backer
- Posts: 1064
- Joined: Tue Jan 06, 2009 4:12 pm
- Programming Language of Choice: C/C++, Java
Re: Multiple inheritance
That's the thing... there are very few areas where you should HAVE to use it that aren't bad design choices. Anyway, multiple inheritance adds ambiguity to member lookups because it has to look at both bases instead of just one. Also, resolving which base's member should be called or used creates even MORE ambiguity. Basically, it makes it hella hard to figure out what's gonna happen
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Re: Multiple inheritance
I agree, for the first time in my allmost 3 years of programming that was the first time I had needed to do so, This was only becasue XNA by default needs to inheit from the Xna.Game class.Ginto8 wrote:That's the thing... there are very few areas where you should HAVE to use it that aren't bad design choices. Anyway, multiple inheritance adds ambiguity to member lookups because it has to look at both bases instead of just one. Also, resolving which base's member should be called or used creates even MORE ambiguity. Basically, it makes it hella hard to figure out what's gonna happen
I guess a good workaround would be to have the inherited class inherit from the second class?
Code: Select all
class B {
}
class ClassA : ClassB {
}
class I_NEED_CLASS_A_AND_B : ClassA {
}
- TheBuzzSaw
- Chaos Rift Junior
- Posts: 310
- Joined: Wed Dec 02, 2009 3:55 pm
- Current Project: Paroxysm
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++
- Contact:
Re: Multiple inheritance
Ginto8 pretty much said it.
You should really deeply question the "need" to use multiple inheritance. There always a better way to solve the problem. It may surprise you how that superior solution comes about: splitting it into two objects, refactoring other aspects of the code, switching to component-based design, etc.
You should really deeply question the "need" to use multiple inheritance. There always a better way to solve the problem. It may surprise you how that superior solution comes about: splitting it into two objects, refactoring other aspects of the code, switching to component-based design, etc.
Re: Multiple inheritance
Not sure I totally agree that the would always be another solution, but I admit if you ever need it then it would be a very rare case scenario.TheBuzzSaw wrote:Ginto8 pretty much said it.
You should really deeply question the "need" to use multiple inheritance. There always a better way to solve the problem. It may surprise you how that superior solution comes about: splitting it into two objects, refactoring other aspects of the code, switching to component-based design, etc.
- TheBuzzSaw
- Chaos Rift Junior
- Posts: 310
- Joined: Wed Dec 02, 2009 3:55 pm
- Current Project: Paroxysm
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++
- Contact:
Re: Multiple inheritance
The point is that you never really "need" it; you can only set yourself up to need it on accident. Imagine you are writing the program in C# or Java where multiple inheritance is outright banned. What would you do to fix it?
- Ginto8
- ES Beta Backer
- Posts: 1064
- Joined: Tue Jan 06, 2009 4:12 pm
- Programming Language of Choice: C/C++, Java
Re: Multiple inheritance
If you find yourself in a situation where you'd use multiple inheritance, the best choice is usually to use composition for one of the objects instead of inheritance
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
- dandymcgee
- ES Beta Backer
- Posts: 4709
- Joined: Tue Apr 29, 2008 3:24 pm
- Current Project: https://github.com/dbechrd/RicoTech
- Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
- Programming Language of Choice: C
- Location: San Francisco
- Contact:
Re: Multiple inheritance
Exactly, I assure you there is no multiple inheritance in assembly.TheBuzzSaw wrote:The point is that you never really "need" it; you can only set yourself up to need it on accident. Imagine you are writing the program in C# or Java where multiple inheritance is outright banned. What would you do to fix it?
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
- Ginto8
- ES Beta Backer
- Posts: 1064
- Joined: Tue Jan 06, 2009 4:12 pm
- Programming Language of Choice: C/C++, Java
Re: Multiple inheritance
Nor are there classes. Though I see where you're coming from, your justification is a moot pointdandymcgee wrote:Exactly, I assure you there is no multiple inheritance in assembly.TheBuzzSaw wrote:The point is that you never really "need" it; you can only set yourself up to need it on accident. Imagine you are writing the program in C# or Java where multiple inheritance is outright banned. What would you do to fix it?
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Re: Multiple inheritance
I agree with Ginto8, having it as a class member seems to allways be a good workaround.
Strange how everyone here agrees that its not nessacary at all but on most other forums 50/50 agree/disagree. Noobs v experiance?
Strange how everyone here agrees that its not nessacary at all but on most other forums 50/50 agree/disagree. Noobs v experiance?
- thejahooli
- Chaos Rift Junior
- Posts: 265
- Joined: Fri Feb 20, 2009 7:45 pm
- Location: London, England
Re: Multiple inheritance
As much as they might be considered bad design by more experienced programmers, I don't think that they are entirely bad. There are occasions where multiple inheritance is better and using other methods would not be an ideal solution to the problem. Feel free to contradict as I admit that I am not as experienced as most people on here at programming.
I'll make your software hardware.
- TheBuzzSaw
- Chaos Rift Junior
- Posts: 310
- Joined: Wed Dec 02, 2009 3:55 pm
- Current Project: Paroxysm
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++
- Contact:
Re: Multiple inheritance
The biggest thing you have to consider is that inheritance implies an is-a relationship. So, the question quickly becomes: why is A both a B and a C but B is not a C? Yes, there are situations where it is convenient to bring on multiple layers. In smaller programs, you can often get away with this.thejahooli wrote:As much as they might be considered bad design by more experienced programmers, I don't think that they are entirely bad. There are occasions where multiple inheritance is better and using other methods would not be an ideal solution to the problem. Feel free to contradict as I admit that I am not as experienced as most people on here at programming.
- Ginto8
- ES Beta Backer
- Posts: 1064
- Joined: Tue Jan 06, 2009 4:12 pm
- Programming Language of Choice: C/C++, Java
Re: Multiple inheritance
but then you have big programs, where it fucks things up. The only exception is when C is an interface (abstract class with no member data in it for those C++ers who haven't ventured out into the world of other OOP languages ), in which case you can have as many Cs as you want, thought it may make the vtbl a bit bloatedTheBuzzSaw wrote:The biggest thing you have to consider is that inheritance implies an is-a relationship. So, the question quickly becomes: why is A both a B and a C but B is not a C? Yes, there are situations where it is convenient to bring on multiple layers. In smaller programs, you can often get away with this.thejahooli wrote:As much as they might be considered bad design by more experienced programmers, I don't think that they are entirely bad. There are occasions where multiple inheritance is better and using other methods would not be an ideal solution to the problem. Feel free to contradict as I admit that I am not as experienced as most people on here at programming.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Re: Multiple inheritance
I don't think anyone is calling multiple inheritance a bad design, I think they're calling it already shitty design if you get to the point where you need it.thejahooli wrote:As much as they might be considered bad design by more experienced programmers, I don't think that they are entirely bad. There are occasions where multiple inheritance is better and using other methods would not be an ideal solution to the problem. Feel free to contradict as I admit that I am not as experienced as most people on here at programming.
- Falco Girgis
- Elysian Shadows Team
- Posts: 10294
- Joined: Thu May 20, 2004 2:04 pm
- Current Project: Elysian Shadows
- Favorite Gaming Platforms: Dreamcast, SNES, NES
- Programming Language of Choice: C/++
- Location: Studio Vorbis, AL
- Contact:
Re: Multiple inheritance
Eh, it's just a different style of OO design.
Saying C++ allowing multiple inheritance is "poor design" is really stupid. There are many extremely useful applications for it. The downsides are that it makes things more complex, and there are ambiguities.
Java, C#, and these "single inheritance, multiple interface" OO languages simply favor a different style. They want you to strictly enforce a difference between interface and implementation.
edit: Especially now that I've spent such a considerable amount of time developing with QT... there are some SERIOUSLY fucking convenient/powerful (though questionable) things you can do with multiple inheritance, some casting, and signals and slots.
Here's a snippit from some toolkit code:Poor design? I'll let you decide. The amount of things that QT now handles for me and the thousands of lines of code I can save with this kind of abuse and some clever casting is so fucking worth it.
Saying C++ allowing multiple inheritance is "poor design" is really stupid. There are many extremely useful applications for it. The downsides are that it makes things more complex, and there are ambiguities.
Java, C#, and these "single inheritance, multiple interface" OO languages simply favor a different style. They want you to strictly enforce a difference between interface and implementation.
edit: Especially now that I've spent such a considerable amount of time developing with QT... there are some SERIOUSLY fucking convenient/powerful (though questionable) things you can do with multiple inheritance, some casting, and signals and slots.
Here's a snippit from some toolkit code:
Code: Select all
class Entity: public QWidget, public TreeNode, public QGraphicsRectItem, AssetManager {