Why does erasing from this vector crash my program?

Started by
11 comments, last by DevFred 13 years, 11 months ago
If you take advantage of C++0x features, you might as well go ahead and use a std::vector<std::unique_Pointer<Projectile>> ;-)
Advertisement
I would make a separate named function for the update process - it seems important enough. And why not use a boost::ptr_vector<Projectile>? :) It seems clear that the purpose of the pointers here is to indirect for polymorphism, so that's the natural fit IMO.

struct Update {  float interval;  Update(float interval): interval(interval) {}  bool operator()(Projectile& p) {    p.Move(interval);    return !p.inBounds();  }};// ...typedef boost::ptr_vector<Projectile> projectiles_t;projectiles_t projectiles;// ...projectiles_t::iterator begin = projectiles.begin(), end = projectiles.end();projectiles.erase(std::remove_if(begin, end, Update(deltaT)), end);


Writing things this way separates responsibilities: updating an object vs. applying the removal algorithm vs. memory management.
I find predicates with side-effects disturbing, but maybe that's just the Haskell part of my brain speaking :)

This topic is closed to new replies.

Advertisement