A
std::vector is an STL container that wraps a dynamic array, a data structure using contiguous storage. When you call
push_back() and its capacity (= maximal number of elements that fit into the vector) is exhausted, the container will trigger a reallocation, i.e. allocate a bigger array. In this step, elements are copied or moved from the old container to the new, bigger one, which invalidates all pointers, references and iterators to the old elements.
Instead of
std::vector<std::unique_ptr<T>>, it would be easier to use a different container such as
std::list<T> or
std::deque<T>.
And don't use indices to iterate through the container, that's the purpose of iterators. Random access is not supported by all container types. By the way, the correct type for indices and sizes would be
std::size_t, not
int. An iterator loop looks as follows:
for (std::vector<...>::iterator itr = _projectileArray.begin();
itr != _projectileArray.end(); ++itr)
{
itr->update();
CheckBulletCollision(*itr);
}
With C++11, you can deduce the type by replacing
std::vector<...>::iterator with
auto, or even better, use the range-based for loop:
for (Projectile& p : _projectileArray)
{
p.update();
CheckBulletCollision(p);
}
Some general tips: Make your member variables
private, don't duplicate textures for each object, use the constructor initializer list, don't define unnecessary destructors, consider the rule of three, and use
std:: before standard library functions such as
cos().