I have a Map over which I iterate like this:
std::map<unsigned int, GameObject *>::iterator itr = _gameObjects.begin();
while (itr != _gameObjects.end())
{
itr->second->Update();
itr++;
}
Update() might insert an element into the map or even remove one from it, but it doesn’t necessarily do any of the two.
It obviously doesn’t work like that. Is there a way it can be done?
From
std::map::erase():From
std::map::insert():From
std::map::operator[]:If
Update()does not remove itself, then the code is legal. IfUpdate()does, then it is not.Update()would be required to inform the calling code if it removed itself, either setting a flag or returning the next iterator (as suggested by Attila).