Creating objects

Discussion in 'C++' started by ikl, Apr 7, 2004.

  1. ikl

    ikl Guest

    When creating a list of objects of the same class, what should be concerned
    to decide if using "new" or not? Since how many number of the objects are
    unknown until runtime, probably it is not a good idea to create like:

    class A;

    A a[SIZE];
    ....

    How would like to code this? Thanks!
    ikl, Apr 7, 2004
    #1
    1. Advertising

  2. "ikl" <> wrote in message
    news:nHPcc.30086$...
    > When creating a list of objects of the same class, what should be

    concerned
    > to decide if using "new" or not?


    You should almost never use new. If you need dynamic allocation of objects
    use an STL container. E.g.

    std::list<A> my_list;
    std::vector<A> my_vector;

    The advantages of these are that you get a rich set of functionality,
    instead of prmitive arrays, and you don't have to remember to clean up these
    object you won't get memory leaks.

    > Since how many number of the objects are
    > unknown until runtime, probably it is not a good idea to create like:
    >
    > class A;
    >
    > A a[SIZE];
    > ...
    >
    > How would like to code this? Thanks!
    >


    std::vector<A> my_vector(SIZE);

    john
    John Harrison, Apr 7, 2004
    #2
    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:
    398
    Michael Borgwardt
    Nov 18, 2003
  2. Simon Elliott

    Inheritance of objects within objects

    Simon Elliott, Dec 10, 2004, in forum: C++
    Replies:
    2
    Views:
    331
    Simon Elliott
    Dec 10, 2004
  3. =?Utf-8?B?c3RldmVuIHNjYWlmZQ==?=

    form objects into class objects

    =?Utf-8?B?c3RldmVuIHNjYWlmZQ==?=, Jul 5, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    432
    =?Utf-8?B?c3RldmVuIHNjYWlmZQ==?=
    Jul 6, 2006
  4. 7stud
    Replies:
    11
    Views:
    688
    Dennis Lee Bieber
    Mar 20, 2007
  5. News123
    Replies:
    7
    Views:
    378
    Dave Angel
    Jun 29, 2009
Loading...

Share This Page