J
Janina Kramer
hi ng,
i'm working on a multiplayer game for a variable number of players and on the client side, i'm using a std::vector<CPlayer> to store
informatik about the players. CPlayer is a class that contains another std::vector<CPosition>. Because one of the players is the
client itself (and the size of the vector<CPlayer> doesn't change during a game), i thought i could store a
std::vector<CPlayer>::iterator "localplayer" that points to the respective element of the vector.
The strange thing is that when i add elements to the std::vector<CPosition> (which is nothing more than a usual member of CPlayer)
the iterator "localplayer" becomes somehow invalid (in a way that the memory it points to is no longer the actual "localplayer" but
rather some random position in memory). what's wrong here? i didn't change anything about the std::vector<CPlayer> so why can't i
use the "localplayer" iterator any more? and: would it help to store a reference to the element rather than an iterator? i fixed
this problem by using an index instead of iterator, but i'm really curious about this.
thanks in advance
janina
i'm working on a multiplayer game for a variable number of players and on the client side, i'm using a std::vector<CPlayer> to store
informatik about the players. CPlayer is a class that contains another std::vector<CPosition>. Because one of the players is the
client itself (and the size of the vector<CPlayer> doesn't change during a game), i thought i could store a
std::vector<CPlayer>::iterator "localplayer" that points to the respective element of the vector.
The strange thing is that when i add elements to the std::vector<CPosition> (which is nothing more than a usual member of CPlayer)
the iterator "localplayer" becomes somehow invalid (in a way that the memory it points to is no longer the actual "localplayer" but
rather some random position in memory). what's wrong here? i didn't change anything about the std::vector<CPlayer> so why can't i
use the "localplayer" iterator any more? and: would it help to store a reference to the element rather than an iterator? i fixed
this problem by using an index instead of iterator, but i'm really curious about this.
thanks in advance
janina