Jump to content
  • Advertisement
Sign in to follow this  
raptorstrike

is it nessisary to dynamicly creat inside vectors?

This topic is 5033 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

TOPIC^ should i do something like this block i; Worm_Blocks.push_back(i); or do this block i = new block; Worm_Blocks.push_back(i); dose it matter?

Share this post


Link to post
Share on other sites
Advertisement
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
Unless you're using derived classes and storing the base-class pointer in the vector - for example, a master entity list.

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
yeah but i have to refrence to it latter in the function and still maintain the previous .back for comparison perposes (so i cant just do .back)

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!