PDA

View Full Version : Dynamic Names of Classes?



ImpactDNI
04-18-2003, 06:03 PM
This doesn't have all that much to do with OGL but you guys are great at answering questions =P
ok, so i have a class called "model"
and i have an open .ase file... in the first few lines there is a string that is the name of the model contained in the .ase file... i read that in with a
char modelinc[255];
fscanf(s, "%s", &modelinc);
where s is the name of my file, and modelinc is the name of the incomming model.. but now i want to define a "model" (my class) for the rest of the data, so lets say its a cube... it would normally just be
model cube;
but i want "cube" above to be dynamic, based on what comes in... ive tried
model modelinc;
but this gives me a redefined error... ive tried all sorts of
model *modelname;
fscanf(s, "%s", &modelinc);
modelname = modelinc;
none of this works...
someone gimme some help???
Thanks in advance!

shinpaughp
04-18-2003, 06:31 PM
No, you can't. Use linked list(inefficient), arrays, <vector> maybe, but you cannot have a dynamically changing variable name. Some sort of pointer or dynamic memory allocation.

[This message has been edited by shinpaughp (edited 04-18-2003).]

ImpactDNI
04-18-2003, 07:34 PM
I dont need it to change once it is defined, it just needs to be defined by a variable...
any way to do that?

shinpaughp
04-18-2003, 07:41 PM
In C, use malloc, in C++ use new.

shinpaughp
04-18-2003, 07:59 PM
How about this, in your model class have a simple parser to determine the extension, thereby determining the model type, and have a multitude of model loader member functions.

model* modelname;
fscanf(s, "%s", &modelinc);
modelname = new model(modelinc);

and you have an overloaded constructor which accepts in the filename, parses it, and calls the appropriate loader function.


[This message has been edited by shinpaughp (edited 04-18-2003).]

ImpactDNI
04-19-2003, 05:09 AM
so after i use the
new model(modelname);
then i have to call some function to give it like
cube.verts
cube.faces
all those?
and those i do by what command?
modelname = new vert3(verts);
is that right? (vert3 is another one of my classes =P)

jebus
04-19-2003, 08:41 AM
Originally posted by ImpactDNI:
modelname = new vert3(verts);

this will not work. going along with your previous example, modelname is a pointer to a model. new vert3() returns a pointer to an array of vert3's. if your model has verts, then you would use:

modelname.verts = new vert3(<number of verts> ); // without the <>

this is basic C/C++ stuff. you may want to read up on memory allocation and pointers. good luck!

jebus

shinpaughp
04-19-2003, 09:38 AM
If you want a specific class for each model type then you would create a base class (model) with all similar attributes, and then derive from the base class each model type with its unique or particular attributes. Then you could probably have a function (probably standalone or part of a container type class) to parse the filename and call something like:

model* anothermodel = new derivedmodel(filename);

Then you have specific classes for each model type. Because derivedmodel is derived from model it can be assigned to a model pointer which makes it possible to have arrays of baseclass and derivedclass and C++ will see them as what they are not just the base class. etc, etc, etc

But, I agree with Jebus, read up on pointers and memory allocation and inheritance and C++ in general.