Advertisement

type information

Started by January 14, 2004 04:54 AM
2 comments, last by BrennendeKomet 21 years, 1 month ago
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

tree
spaceship
house
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!!!
C++ RULEZ!!! :)
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 typetypedef CMyBaseClass * (*TypeFunc) (void);//create an initialisator array of size N_TYPES#define N_TYPES 1TypeFunc typefunctions[N_SCENES];//declare the tree initialisation functionCMyBaseClass *InitTree(void);//assign the first element (ID=0) of the initialisator array to the tree initialisator:typefunctions[0]=InitTree;//define the tree initialisator:CMyBaseClass *InitTree(void){  CMyBaseClass tree[1];  return &tree[0];}

I hope this is clear .
Advertisement
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);map<FunctionPtr,CString>


the rest is clear

C++ RULEZ!!!
C++ RULEZ!!! :)
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]

This topic is closed to new replies.

Advertisement