variable nr of objects

V

v.niekerk

Hi,

I would like to be able to create a variable number of objects. So, when
there's a class Thing, instead of

Thing thing1, thing2, thing3

do like

cin >> nr;
for (x = 0; x < nr; x--)
{
Thing thing[x];
}

I searched the net for info but found no answer and tried but get errors.
Is this possible anyway?

Thanks.
 
H

Huub

Hi,

I would like to be able to create a variable number of objects. So,
when there's a class Thing, instead of

Thing thing1, thing2, thing3

do like

cin>> nr;
for (x = 0; x< nr; x--)
{
Thing thing[x];
}

I searched the net for info but found no answer and tried but get
errors. Is this possible anyway?

Thanks.

Perhaps something like:

std::cin >> nr;
std::vector<Thing> items(nr);
// do stuff with items

/Leigh

Thank you for answering, but I don't fully understand.

Do you mean items(nr) as an array ?
 
H

Huub

On 28/04/2011 15:08, (e-mail address removed) wrote:
Hi,

I would like to be able to create a variable number of objects. So,
when there's a class Thing, instead of

Thing thing1, thing2, thing3

do like

cin>> nr;
for (x = 0; x< nr; x--)
{
Thing thing[x];
}

I searched the net for info but found no answer and tried but get
errors. Is this possible anyway?

Thanks.

Perhaps something like:

std::cin>> nr;
std::vector<Thing> items(nr);
// do stuff with items

/Leigh

Thank you for answering, but I don't fully understand.

Do you mean items(nr) as an array ?
You seem a bit lost: buy a couple of C++ books such as "The C++
Programming Language" by Stroustrup.

/Leigh

I have that book and searched it for this subject. Will search again...
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top