Creating objects

I

ikl

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!
 
J

John Harrison

ikl said:
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
 

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

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top