variable nr of objects

Discussion in 'C++' started by v.niekerk@hccnet.nl, Apr 28, 2011.

  1. Guest

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

  2. Huub Guest

    On Thu, 28 Apr 2011 15:14:42 +0100, Leigh Johnston wrote:

    > On 28/04/2011 15:08, 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 ?
    Huub, Apr 28, 2011
    #2
    1. Advertising

  3. Huub Guest

    On Thu, 28 Apr 2011 15:25:14 +0100, Leigh Johnston wrote:

    > On 28/04/2011 15:22, Huub wrote:
    >> On Thu, 28 Apr 2011 15:14:42 +0100, Leigh Johnston wrote:
    >>
    >>> On 28/04/2011 15:08, 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...
    Huub, Apr 28, 2011
    #3
    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:
    389
    Michael Borgwardt
    Nov 18, 2003
  2. Simon Elliott

    Inheritance of objects within objects

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

Share This Page