Page 1 of 2

Multiple inheritance

Posted: Fri Jan 14, 2011 1:48 pm
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.

Re: Multiple inheritance

Posted: Fri Jan 14, 2011 2:29 pm
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

Re: Multiple inheritance

Posted: Fri Jan 14, 2011 2:56 pm
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 {

}

Re: Multiple inheritance

Posted: Fri Jan 14, 2011 5:31 pm
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.

Re: Multiple inheritance

Posted: Fri Jan 14, 2011 6:34 pm
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.

Re: Multiple inheritance

Posted: Fri Jan 14, 2011 7:15 pm
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? ;)

Re: Multiple inheritance

Posted: Fri Jan 14, 2011 8:11 pm
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 ;)

Re: Multiple inheritance

Posted: Fri Jan 14, 2011 9:21 pm
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.

Re: Multiple inheritance

Posted: Fri Jan 14, 2011 9:25 pm
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:

Re: Multiple inheritance

Posted: Tue Jan 18, 2011 12:05 pm
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?

Re: Multiple inheritance

Posted: Tue Jan 18, 2011 1:32 pm
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.

Re: Multiple inheritance

Posted: Tue Jan 18, 2011 4:21 pm
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.

Re: Multiple inheritance

Posted: Tue Jan 18, 2011 9:35 pm
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

Re: Multiple inheritance

Posted: Wed Jan 19, 2011 9:05 am
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.

Re: Multiple inheritance

Posted: Wed Jan 19, 2011 3:54 pm
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.