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]