How to avoid double inherritance

Started by
7 comments, last by Ademan555 20 years, 1 month ago
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]
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out his new home he rounded a corner and saw someone in Marine Dress Blues. He ran back to St. Peter and yelled "You lied to me! There are Marines in heaven!" St. Peter said "Who him? That's just God. He wishes he were a Marine."
Advertisement
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.
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
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out his new home he rounded a corner and saw someone in Marine Dress Blues. He ran back to St. Peter and yelled "You lied to me! There are Marines in heaven!" St. Peter said "Who him? That's just God. He wishes he were a Marine."
using .net compiler? your wincontainer could be static.
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.
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.
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.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
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
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
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.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/

This topic is closed to new replies.

Advertisement