variable nr of objects

H

Huub

Hi,

I would like to be able to create a variable number of an object. 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 tried but get errors. Is this possible anyway?

Thanks.
 
H

Huub

Hi,

I would like to be able to create a variable number of an object. 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 tried but get errors. Is this possible anyway?

Thanks.

I searched on cprogramming.com and the C++ book by Stroustrup, but
couldn't find anything about this.
 
W

Werner

Hi,

I would like to be able to create a variable number of an object. 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 fear commenting on the code here above might be
useless. Why are you decrementing? If x is greater
or equal to nr, you will never enter the loop. This
means to enter the loop nr has to be positive (as x
is initially 0). If you enter the loop :), you will
never exit it, as decrementing x will never cause the
invariant to break.

To my knowledge to create an array of objects
(or things), the amount of items in the array
needs to be available at compile time. Therefore
this code won't work.

Furthermore, I imagine you are aware that the "things"
are created every time the loop is entered (apart from
the fact that it won't work).
I tried but get errors. Is this possible anyway?

What errors did you get?

Remedy: Use a vector.

#include <vector>
#include <iostream>

struct Thing
{
};

int main()
{
int result = -1;
int count;
if( std::cin >> count )
{
//This creates a sequence of count
// things, accessible through operator [],
// as with a normal array...
std::vector<Thing> things( count );
for( int i = 0; i < count; ++i )
{
things;//works fine...
}
result = 0;
}
return result;
}

Note:

If count is negative or zero, the loop simply won't
be entered. If user input is erroneous, the state
of cin (returned from cin >> count) will evaluate
to false.

Regards,

Werner
 
S

Stuart Redmann

I would like to be able to create a variable number of an object. 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 tried but get errors. Is this possible anyway?

Yes, it is possible. Let's give you a short introduction.

There are two kinds of arrays under C++, one whose size is known at
compile time (IOW, the size is a constant that is put into the code)
and one kind where the size of the array depends on some input at run-
time (which is your case). While the first kind can be allocated in
the way you have tried ("Thing thing[x];", where x must be a
constant), the second kind can only be allocated using "new", for
example

Thing* thing = new Thing (x);

Note that arrays that are allocated with "new" must be released
manually by a matching call to "delete[]" (note that the empty
brackets are intentional).

The reason why this is so is rather complex and has to do with how the
compiler internally managed the memory (google for "heap versus
stack").

The solution that Leigh had posted in the other branch of this thread
is pretty much the standard way to work with arrays under C++.

On 28 Apr., Leigh Johnston wrote:
[snip]
std::cin >> nr;
std::vector<Thing> items(nr);
// do stuff with items

The class "vector" relieves you from having to manage the memory by
yourself. Note that "items" in above example is not an array but a
single object. This object will use an array internally (so
std::vector will perform the "new" and "delete[]" calls for you).
Apart from that, std::vector can do a lot more, such as re-allocating
the underlying array if necessary.

I'd suggest to try the "new"-allocated array first, so that you get an
understanding how arrays work under C++. Then you should replace this
with std::vector, since this is most reliable.

Regards,
Stuart
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top