C++ Class

Questions about programming languages and debugging
Post Reply
User avatar
visser
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 472
Joined: 03 Apr 2007, 16:00
17
Location: online
Contact:

C++ Class

Post by visser »

So I have a question that I am going to be digging into within the next few minutes here but I wanted to post on suck-o first:

here is the idea behind this: We want to create a program that will create a D&D character by using a class and be able to output that characters stats to screen as well as a file. The user should be able to create multiple characters at once.

I am going to have no problem with to much of this. I know how to create multiple instances of the same class:

Code: Select all

ClassNameHere classInstance1;
ClassNameHere classInstance2;
ClassNameHere classInstance3;
but what do i do if i dont know how many class instances I want to create? in this case it was easy to just declare the three of them. but what if one user wants 4 and the other wants 14?

any help would be great!
AKA Scottyrabbit

software engineers unite!

//When my words are remembered its because my actions were loud enough.

User avatar
IceDane
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 197
Joined: 12 Aug 2009, 16:00
14

Post by IceDane »

Your question is a bit strange. Normally, the abstraction between the end user and the code is so big that "what the user wants" is not really reflected directly in the code. E.g., if you have a class that performs a purpose, and the user wants to do whatever that class does a few times, he isn't "asking" directly for "x instances of class y."

I hope that makes sense. I have a feeling that your explanation is just badly phrased, anyway, so I'll answer your question.

If you know before hand how many instances of the classes you need to declare, a fixed-size array on the stack of the classes will do. If not, you will need to allocate them dynamically and store them in either an array or an STL container such as a vector. A vector would be preferred.

Code: Select all

// Know how many classes you need before hand
ClassName classArray1[4]; // 4 instances of ClassName in an array
ClassName classArray2[14]; // 14 instances
The other is a bit more complex, though

Code: Select all

// int x, y will contain the number of instances we want
ClassName* array1 = new ClassName[x];
ClassName* array2 = new ClassName[y];
// Optionally, to add to a vector that scaled dynamically in size
// you can do the following
std::vector<ClassName*> classVector;
// Inserts all elements beteween, and including array1[0] and array1[x-1]
classVector.insert(classVector.begin(), array1, array1+x-1);
// Same as above 
classVector.insert(classVector.begin(), array2, array2+y-1);

// Do stuff with the above instances

delete [] array1;
delete [] array2;

// Important to note that ClassVector is unusable now. 
This may look complex, but it really depends on your usage scenario how complex it has to be. It may be easier to allocate one instance of the class dynamically at a time when needed and add it to the vector, in which case you can free them up individually at the end, without having to worry about the vector possibly being unusable at some point and whatnot.

Smart pointers will also be of great use.

If you explain your need a bit better, I may be able to come up with a solution that will suit you better. [/code]

Post Reply