• Advertisement

Archived

This topic is now archived and is closed to further replies.

Multiple Inheritance Bad?

This topic is 5592 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I remember someone said that it is a bad idea to have multiple inheritance, but why was it? I am designing a 2D sprite system and want to keep it as less API dependant as possible. So I was thinking to have a abstract data type Sprite. Then, if a derived sprite uses Direct3D it would inherit from Sprite but also would need to inherit some Direct3D functionality also. That way, say I have two different sprite class, one that uses D3D and another that uses OGL, any object that uses a sprite would be kept generalize, having only a pointer to Sprite and not worry about which API is in question. The problem is still with the specific D3DSprite or OGLSprite. Either would inherit from Sprite, but would also need to inherite from some sort of D3D or GL type object in order to be able to draw itself. Does anyone have any idea? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
I don''t know who told you multiple inheritence is bad. My guess would be a Java programmer making excuses for the lack of multiple inheritence in Java. Anyway...

Your idea would work, but think about this approach. Create Sprite class, a D3DTarget class, and an OGLTarget class. Both ???Target classes would be Singletons. Next make a D3DSprite class and a OGLSprite class which are derived from your Sprite class. The sprite class would contain an interface for rendering the sprite. The D3DSprite and OGLSprite would implement the sprite rendering interface inheriteted from the Sprite class, using a pointer to the D3DTarget or OGLTarget Singleton class.

I hope this makes sense. Email me (dvogel [at] intercarve [dot] net) if you need any further explanation.


Regards,
Drew Vogel

Share this post


Link to post
Share on other sites
There''s nothing wrong with multiple inheritance, it''s just tricky to get right, especially in C++.

One reason, and this is apparent from your example, is that it''s sometimes used unwisely as a replacement for composition. I see no good reason why a Direct3D sprite needs to inherit from some sort of Direct3D object as well as a Sprite object. It is a sprite but it uses Direct3D. All the specialisation can be done in the derived classes of Sprite without having those derived classes needing to derive from API-specific classes too. All your classes need is some sort of reference to a Direct3D device so that they can call the appropriate functions. That can be a singleton, or passed in via the sprite''s constructor, or perhaps just passed as a parameter when the sprite needs it.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]

Share this post


Link to post
Share on other sites
Nothing wrong with most features of C++ - just most C++ programmers don''t have a clue how to use them and therefore end up creating horrible code. Multiple Inheritance is one of those great sounding features that most people use without knowing why or what they are using it for.

IMHO Multiple Inheritance is no loss to the world in the current generation of modern languages. I don''t miss it.

And if I ever have to debug another badly designed system that uses MI I''ll probably rip my hair out! :-)


Merry Xmas

Share this post


Link to post
Share on other sites
i guess the only reason why people call it bad is that some programmers get carried away and try to use it even when its not making much sense *g*
and of course a few dangerous things. imagine b and c inherit from a and x inherits from b and c. you would have all the stuff of a in x too.. but twice. except youre aware of it and know how to avoid it ,-)

Share this post


Link to post
Share on other sites
Well, that''s what you use virtual inheritance for in C++...

-Markus-

Share this post


Link to post
Share on other sites
*g* as i said.. except youre aware of it.. but for my taste there are too many things to keep in mind.. i dont want to think about all the time that can be spent with debugging because it was used when it wasnt necessary...

Share this post


Link to post
Share on other sites
I''ve only used MI for my smaller helper classes... I have a templated counter class, to keep track of how many instances I have and what instance I''m currently playing with. And a templated Singleton class. And I''m sure I can come up with a few others But for me MI is nice to use, sometimes. And for me those two example are valid. But I''m sure there are people who disagree

"No lies of sugar can sweeten the sournes of reality"

}+TITANIUM+{ A.K.A. DXnewbie[onMIRC]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:

I remember someone said that it is a bad idea to have multiple inheritance, but why was it?



The greater the chance a particular construct has to screw up your program, the greater the care that must be taken in applying said construct. Consequently, there are quite a few that argue against these constructs altogether. Philosophical and idealogical reasons also factor into the decision.

On the other hand, practical reasons also weigh in. Often times what you want is to inherit structure along a single path and introduce interface from others. Thus Java has single inheritance with interfaces, Objective-C has single inheritance with protocols, Ruby has single inheritance with mix-ins, etc. C++ provides the basic inheritance feature without restrictions. So while MI can be useful, tread carefully and thoughtfully. Also read up on virtual inheritance. Hope it works out.

Share this post


Link to post
Share on other sites
To call back on the original problem. I have a Sprite class so that objects that need to use sprites could just use it and not worry about how it do its work.

D3DSprite of course, inherits from Sprite in its nature. However, D3DSprite involves Direct3D stuff. I could easily pass in the Direct3DDevice and it would be all the Direct3D it need. However, with any Direct3D stuff, there involve Creation/Destruction/Rebuilding/Invalidation of various objects. One example is when you Alt+Tab, you would have to destroy everything and rebuild them.

Thus, I would like to have a generalize class that does such and is abstract so that all Direct3D related class can derive from.

So, is this a good call for multiple inheritance?

Share this post


Link to post
Share on other sites

  • Advertisement