I have a vector that contains Enemy objects called enemiesOnField. When calling a function takeYourTurn that potentially changes the coordinates of individual enemies I noticed that even though the change occurs within the function, it doesn't persist in the enemiesOnField vector. This leads me to believe that when calling this function by iterator it is somehow called on a copy, not the original object. When calling by reference returned by the [] operator everything works normally.
The code that doesn't work as expected is commented out:
void World::letNPCsAct()
{
/* this doesn't work
for (auto it : enemiesOnField)
it.takeYourTurn();*/
// this works
for (int i = 0; i < enemiesOnField.size(); ++i)
enemiesOnField[i].takeYourTurn();
}
Why is this like this??