Why does a static vector have different content in different classes?
I have the following code:
namespace GameStatus{
static std::vector<SavePoint>savePoints;
}
A SavePoint
:
class SavePoint{
public:
int distance;
int score;
SavePoint(int d,int s){
distance = d;
score = s;
};
};
The problem is that even if GameStatus::savePoints
a static
vector appears to be, or contain elements or not, depending on who is calling class.
For example, if I have a class that adds a savepoint:
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 another 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 that alternately executes code in Game and Foo, and it seems that Game
GameStatus::savePoints
there is savePoint in (not empty), while in Foo there is not (appears empty). It doesn't make sense to me because it savePoints
is static. Therefore, both classes must have access to the same object.
Why GameStatus::savePoints
does it have different meanings if different classes?
source to share
static
the keyword has two meanings in C ++.
- inside a class (this is an object oriented value, for example in Java or C #)
- storage specifier (external class, this one is also available in C)
Decision. You have to place the declaration in the header with extern
a storage specifier instead static
, because you want it to be global. After that, you have to put the definition in one .cpp file, which is one compilation unit / object. This way you will have one instance of the vector available from all other compilation units that includes the header.
header file:
namespace GameStatus{
extern std::vector<SavePoint> savePoints;
}
Cpp file:
std::vector<SavePoint> GameStatus::savePoints;
source to share
A static
variable outside the class is only defined in the current compilation unit (the compilation unit is the current source that is being compiled - that is, the current source file and all its included files). When you put a static variable in a header, each source file containing that header gets its own copy.
There are two ways to fix this:
1 - Don't put it - change the keyword static
to "extern", you will refer to a global variable. Then you will need to define a global variable in a single source file. For example:
Title:
extern std::vector<SavePoint>savePoints;
Game.cpp:
std::vector<SavePoint> GameStatus::savePoints;
2 - Place it inside the class - static variables inside the class are created only once. Please note, if you put it inside a class, you will need to declare it in the header and also define it in the same compilation unit, otherwise you will get linker errors. For example:
Title:
class Game {
public:
std::vector<SavePoint>savePoints;
}
Game.cpp:
std::vector<SavePoint> Game::savePoints;
source to share
Trust me, you put yours static vector
in a header file and include in classes Game
and Foo
.
So, you need to be clear about what wt means first static
.
static
there are variables for the "lifetime" of the translation unit in which it is defined.
For details see.
As you did, when you include the header, vector
two separate instances will be launched. So these two instances act distinctly. This is why GameStatus::savePoints
they have different meanings if different classes.
You can do two things.
- define a vector as a global variable.
- Class a
singleton
for wrapping your vector.
For a singleton, refer to this .
source to share