views:

37

answers:

1

I have a vector to hold objects of a bullet class. Is this the correct way to add bullets to the vector structure?

std::vector<Bullet> bullets;

Bullet newbullet(thisPlayer.x+PLAYERSPRITEWIDTH,(thisPlayer.y-(PLAYERSPRITEHEIGHT/2)));
bullets.push_back(newbullet);

I don't think the bullets get added this way.

A: 

Thats a perfectly valid way to add "Bullet"s to a std::vector.

Make sure your vector is defined outside of the scope of the function. Otherwise the vector drops out of scope and is deallocated.

Some links that may help your understanding a bit: http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Mips/stack.html
http://en.wikipedia.org/wiki/Scope_(programming) http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization

Goz
Thanks for the responses. I've figured it out now.
lightnin2211