variable nr of objects

Discussion in 'C++' started by Huub, Apr 28, 2011.

  1. Huub

    Huub Guest

    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.
    Huub, Apr 28, 2011
    #1
    1. Advertising

  2. Huub

    Huub Guest

    On Thu, 28 Apr 2011 14:12:01 +0000, Huub wrote:

    > 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.
    Huub, Apr 28, 2011
    #2
    1. Advertising

  3. Huub

    Werner Guest

    On Apr 28, 4:12 pm, Huub <> wrote:
    > 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
    Werner, Apr 28, 2011
    #3
  4. On 28 Apr., Huub wrote:
    > 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
    Stuart Redmann, Apr 29, 2011
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bigbinc
    Replies:
    3
    Views:
    396
    Michael Borgwardt
    Nov 18, 2003
  2. Simon Elliott

    Inheritance of objects within objects

    Simon Elliott, Dec 10, 2004, in forum: C++
    Replies:
    2
    Views:
    330
    Simon Elliott
    Dec 10, 2004
  3. 7stud
    Replies:
    11
    Views:
    682
    Dennis Lee Bieber
    Mar 20, 2007
  4. mfglinux
    Replies:
    11
    Views:
    695
    Roberto Bonvallet
    Sep 12, 2007
  5. David Filmer
    Replies:
    19
    Views:
    237
    Kevin Collins
    May 21, 2004
Loading...

Share This Page