Multiple inheritance

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Multiple inheritance

Post by N64vSNES »

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.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Multiple inheritance

Post by Ginto8 »

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.
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: Multiple inheritance

Post by N64vSNES »

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 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.

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 {

}
User avatar
TheBuzzSaw
Chaos Rift Junior
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

Post by TheBuzzSaw »

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.
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: Multiple inheritance

Post by N64vSNES »

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.
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.
User avatar
TheBuzzSaw
Chaos Rift Junior
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

Post by TheBuzzSaw »

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? ;)
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Multiple inheritance

Post by Ginto8 »

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.
User avatar
dandymcgee
ES Beta Backer
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

Post by dandymcgee »

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? ;)
Exactly, I assure you there is no multiple inheritance in assembly.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Multiple inheritance

Post by Ginto8 »

dandymcgee wrote:
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? ;)
Exactly, I assure you there is no multiple inheritance in assembly.
Nor are there classes. Though I see where you're coming from, your justification is a moot point :mrgreen:
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.
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: Multiple inheritance

Post by N64vSNES »

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?
User avatar
thejahooli
Chaos Rift Junior
Chaos Rift Junior
Posts: 265
Joined: Fri Feb 20, 2009 7:45 pm
Location: London, England

Re: Multiple inheritance

Post by thejahooli »

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.
User avatar
TheBuzzSaw
Chaos Rift Junior
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

Post by TheBuzzSaw »

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.
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.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Multiple inheritance

Post by Ginto8 »

TheBuzzSaw wrote:
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.
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.
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 bloated
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.
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: Multiple inheritance

Post by N64vSNES »

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.
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.
User avatar
Falco Girgis
Elysian Shadows Team
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

Post by Falco Girgis »

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:

Code: Select all

class Entity: public QWidget, public TreeNode, public QGraphicsRectItem, AssetManager {
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.
Post Reply