Declaring an instance

M

Michael Bell

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:
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

Michael Bell

--
 
V

Victor Bazarov

Michael said:
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
 
O

osmium

Michael Bell said:
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:
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?

Do it the way shown below.
ClassName InstanceName1;
ClassName InstanceName2;
ClassName InstanceName3;
// etc

The question makes little sense to me, you ask how to do something and then
show an example of the way to do it. Perhaps your problem is you are
starting with an essentially useless class. All it's members are private
and thus inaccessible to the outside world. Use a *real* example from your
book and see what problems you have.
 
M

Michael Bell

In message <[email protected]>
I believe you meant
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.

Thank you for this. I've tried long and hard to get to these simple
answers in the text books.

Do I take it that in the middle example I am accessing sevenObjects[5]
and assigning the value 42 to the .i of it? That's the kind of
ambiguity you get in textbook examples. They try to teach two points
at once and you, the reader doesn't know which way to take it.

Michael Bell


--
 
J

Jim Langston

Michael Bell said:
In message <[email protected]>
I believe you meant
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.

Thank you for this. I've tried long and hard to get to these simple
answers in the text books.

Do I take it that in the middle example I am accessing sevenObjects[5]
and assigning the value 42 to the .i of it? That's the kind of
ambiguity you get in textbook examples. They try to teach two points
at once and you, the reader doesn't know which way to take it.

Yes. sevenObjects[5] refers to the 6th one in the array (arrays in C and
C++ are 0 bound). the .i referers to the i member and assigns the value
42.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top