I have the following code:
namespace GameStatus{
static std::vector<SavePoint>savePoints;
}
A SavePoint
is :
class SavePoint{
public:
int distance;
int score;
SavePoint(int d,int s){
distance = d;
score = s;
};
};
The problem is, that even though GameStatus::savePoints
is static
, the vector appears to either contain elements or not, depending on who the calling class is.
For example, if I have a class which adds a save point:
class Game{
void addSavePoint(){
SavePoint savepoint(12,10);
GameStatus::savePoints.push_back(savepoint);
}
void testView(){
if(!GameStatus::savePoints.empty()){
Log("There is a save point"); // Can see that savePoints is not empty in the debugger. Everything looks ok here.
}
}
}
But if I have some other class:
class Foo{
void something(){
if(GameStatus::savePoints.empty()){ // In this class it appears that the vector is empty.
//..do something.
}
}
}
I have a game loop which alternately executes code in Game and Foo, and it appears that in Game
GameStatus::savePoints
has a savePoint(is not empty), while in Foo it doesn't (appears empty). It makes no sense to me, because savePoints
is static. So both classes should be accessing the same object.
Why does GameStatus::savePoints
appear to have different values if different classes ?