Sign in to follow this  

is it nessisary to dynamicly creat inside vectors?

This topic is 4814 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

It depends if your vector is a vector of objects or object pointers. Usually you can just specify objects, but be careful for non-POD types, since you need the copy constructor and assignment operator working properly. I tend to use a vector of object pointers for complex types.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Stick with the first one :) why use the second one? 1) It more complex 2) You will have to remember to delete it to free memory 3) acessing it will be slower

Share this post


Link to post
Share on other sites
Quote:
Original post by raptorstrike
4)the other way gives me errors any way

"The other way" is correct if you have a vector<block*> rather than a vector<block>, which you can perfectly well do. In most cases, however, you don't want to.

Share this post


Link to post
Share on other sites
Ooops - should have added - if you are storing dynamic objects, instead of having to think of a new unique name for each you can just use:

worm_blocks.push_back(new block());

Share this post


Link to post
Share on other sites
huh ?? you should be able to do exactly the same things with dynamically allocated objects as you can with static objects. well, i guess technically they aren't static, since the vector is doing the allocating for you...

the point is though, you can do the same stuff. in some situations, using pointers is required... for example, if you want to take advantage of polymorphism (you will one day). for polymorphism to work, dynamic memory is a must.

Share this post


Link to post
Share on other sites

This topic is 4814 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this