Jump to content
  • Advertisement


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


type information

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

Hi all! This is a C++ question, however i am not sure if it really somehow deals with type information. The problem is that i want to have a "command file", where i say write the name of the objects to be created. For example: if we have a file: command.myf
then, when i read this file in my program, i need to create: CMyTree object for "tree", CMySpaceship object for "spaceship" and a CMyHouse object for "house". (It may be that, say, CMyTree and CSpaceship are derived from the same base class but it really doesn''t matter) Well, the one solution is to use a switch statement in my program, or a series of if-else connections. But it is cumbersome, and not that effective... I beleive, this can be done in some way, using a "map" that contains the object ID s (?) and corresponding strings in my file... Then, when i read a statement from the file, i scan the map and retreive the corresponding object ID... but after that i need to create an object of that type... say:
CMyBaseClass* pNewObj = new RETRIEVED_TYPE;
My question is in how to determine "RETRIEVED_TYPE" (from "typeid"?) and what would be the correct syntax for the upper statement? Thank you very much for any help, in advance. Links will also be very appreciated! C++ RULEZ!!!

Share this post

Link to post
Share on other sites
I think you will have to check somehow what type you retrieve or:
make an initialisation function for each type and call and initialize them like this:
//declare the initialisation function type

typedef CMyBaseClass * (*TypeFunc) (void);
//create an initialisator array of size N_TYPES

#define N_TYPES 1
TypeFunc typefunctions[N_SCENES];

//declare the tree initialisation function

CMyBaseClass *InitTree(void);

//assign the first element (ID=0) of the initialisator array to the tree initialisator:


//define the tree initialisator:

CMyBaseClass *InitTree(void){
CMyBaseClass tree[1];
return &tree[0];

I hope this is clear .

Share this post

Link to post
Share on other sites
Thanks for a quick reply!
Yes, i think i got what you mean!
I think the idea will be like that:
there will be functions that create each of totally available objects and return pointer to it. The "map" container will contain pointers to those functions and commands:

typedef CMyBaseClass * (*FunctionPtr)(void);


the rest is clear

C++ RULEZ!!!

Share this post

Link to post
Share on other sites
Hmm, i'd personally choose for IDs as you'll have to check which ID the string belongs to, which costs a lot more time (also, reading the type string costs more time that reading a single int).

[edited by - Tree Penguin on January 14, 2004 8:28:41 AM]

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!