Advertisement Jump to content


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


Class References?

This topic is 6583 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

Ok I don't know if this can be done... but if it can how can it be done? I want to be able to make new classes (using the new operator) using a single function and a table of "references" to classes. Here is what I mean.... Say I have 5 classes derived from CGameUnit, I want to do this:
CGameUnit *MakeUnit(DWORD id)
switch (id)
case 0:
return new CFlyerUnit;

case 1:
return new CGroundUnit;

// etc...

you see where I'm going (right?)... Here is the catch -> I don't want to have to write a new (or rewrite) MakeUnit function every time I add a class... So how can I make a table of refrences to classes so I can simply do this...

CGameUnit *MakeUnit(DWORD id)
 Get Reference Entry from table....
 (maybe a table of class pointers? don't know...)
 return new "ReferencePointer";

Any ideas? or if I need to explain better just say so... Edited by - Ziggy5000 on January 14, 2001 8:42:47 AM

Share this post

Link to post
Share on other sites
I understand what you''re saying now, I think.

Try this...

You want a table of references, right?


// Somewhere in code this is done...?

Units = new CGAMEUNIT[UnitTypeCount];
Units[0] = new CFLYERUNIT;
Units[1] = new CGROUNDUNIT;

CGAMEUNIT* MakeUnit(DWORD which)
if (which > 0 && which < UnitTypeCount)
return Units[which]->Clone();

Then, create a new Clone() method for each unit type that creates a copy of itself.


Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!