#### Archived

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

# How to avoid double inherritance

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

## Recommended Posts

Well, im trying to create my game engine object in a COM sorta way, the only problem is that EVERY direct x component, requires a HWND, and so i was thinking of adding another COM object that would be the HWND and the HINSTANCE and maybe a few other exernal winapi things... but then i realized, say i had my d3d object, and my direct sound object, (and they both were derived from a base class which had the said data) if i then wanted to derive from BOTH of those, a GameEngine object, i would have two sets of the winapi class.... wouldnt ? isnth there some way to avoid this? if you dont see what im saying... here...

WINAPIContainer
/            D3DContainer     DSoundContainer
\          /
EngineObject

[edited by - Ademan555 on March 6, 2004 2:11:33 PM]

##### Share on other sites
You could try inheriting virtually from your WINAPIContainer. Then only one copy should be created no matter how many base objects inherit from WINAPIContainer.

On the other hand, it sounds like the stuff you''re putting in WINAPIContainer should just be in a global variable or something similar.

##### Share on other sites
So when i derive my D3DContainer... i should do this?

class D3DContainer: virtual WINAPIContainer
{
//bleh
};

??? (sorry, ive been programming in c++ for about half a year now... i dont know much about polymorphism, just enough for virtual functions)
thanx
-Dan

##### Share on other sites
using .net compiler? your wincontainer could be static.

##### Share on other sites
If you really want to use the virtual inheritance approach, you need to virtually inherit from the WINAPIContainer in every class that inherits from WINAPIContainer. Also, you probably should publically inherit or it''s not much use.

##### Share on other sites
If you are saying you want to have code like:
class GameEngine : public D3DContainer, DSoundContainer

I''d say that this is a bad use of inheritance. a gameEngine is not a D3DContainer, but instead has a D3DContainer. The difference between IS-A and HAS-A is often used to define where inheritance works and where it doesn''t. If I''m understanding you correctly simply make the D3D and DSound containers members of the GameEngine class.

##### Share on other sites
I agree with cozman. It doesn''t make sense to derive from a class just because it contains data or functions that you need to use. Your classes should contain the class or a reference to the class instead.

Have a single WinApiContainer that contains the Win32 context. Any class that needs to know that info has a reference to the WinApiContainer. Do the same with the D3DContainer and DSoundContainer classes.

Finally, just my opinion but the "Container" in your class names is a poor choice of words. The classes are not what people normally think of as containers.

##### Share on other sites
quote:
Original post by cozman
If you are saying you want to have code like:
class GameEngine : public D3DContainer, DSoundContainer

I''d say that this is a bad use of inheritance. a gameEngine is not a D3DContainer, but instead has a D3DContainer. The difference between IS-A and HAS-A is often used to define where inheritance works and where it doesn''t. If I''m understanding you correctly simply make the D3D and DSound containers members of the GameEngine class.

+1

##### Share on other sites
quote:
if i then wanted to derive from BOTH of those, a GameEngine object...

... then you would be greatly misunderstanding the point of public inheritance.

• 10
• 17
• 9
• 14
• 41