Michael Bell wrote:
I have been going through the textbooks, and they all seem to dodge
what I see as a necessary question: How do you create multiple
instances of the same class?
The problem is that these are teaching books and they only
create one instance, and that is a special case, in real
life you will want to create lots of instances.
Lets's take a simple class declaration
class ClassName
{
struct:
I believe you meant
public:
int i;
char c;
bool b;
//etc
};
That creates the class ClassName. Now I can declare an instance of it
like this:-
ClassName InstanceName;
How do I declare more instances?
ClassName InstanceName1;
ClassName InstanceName2;
ClassName InstanceName3;
// etc
You got it. If you need different names, you give them different names
and then access those instances by using those names.
Another way would be to store the instances in an array:
ClassName sevenObjects[7];
And access individual objects by indexing within the array:
sevenObjects[5].i = 42;
Yet another way is to store the instances in a standard container, say,
a list:
std::list<ClassName> mylist;
ClassName oneObject;
mylist.push_back(oneObject);
mylist.push_back(oneObject);
mylist.push_back(oneObject);
Now the list contains three instances, they are *unnamed* and the only
way to get to them is to _iterate_through_the_list_. I am guessing that
it's a bit advanced for you at this point. All in due time.
V
Thank you for this. I've tried long and hard to get to these simple
answers in the text books.
ambiguity you get in textbook examples. They try to teach two points