Reputation: 1
I am making a game. I have two source files, game.cpp and renderHandler.cpp. I have one header file, gameState.h. The gameState.h file contains a static instance of an enumeration that represents the different game states.
I want to share this static variable with the two source files. I don't want two separate variables in each source file. If I change the value of the game state variable I want it to transfer to the other source file.
gameState.h
#pragma once
enum State {
start,
play,
stop
} static gameState;
game.cpp
#include "../inc/gameState.h"
void Game::init()
{
gameState = State::play;
}
renderHandler.cpp
#include "../inc/gameState.h"
void RenderHandler::render()
{
if (gameState == State::start) {
// code
}
else if (gameState == State::play) {
// code
}
else if (gameState == State::stop) {
// code
}
}
The value of gameState is changed in the game.cpp file. But this does not affect the value of gameState in renderHandler.cpp, it defaults to 0, which I don't want. The value change happens before any of the rendering code is executed.
How do I share a static instance of an enumeration between two source files? Is my logic wrong and should I not use headers and enumerations this way?
Upvotes: 0
Views: 297
Reputation: 5739
Don't share global values directly like this. You may run into static initialization order fiasco, which is really hard to detect.
Use a function instead:
enum State {
start,
play,
stop
};
inline State& gameState() {
static State currentState;
return currentState; // Returns a reference, so you can change the value when you need to
}
game.cpp
#include "../inc/gameState.h"
void Game::init()
{
gameState() = State::play;
}
renderHandler.cpp
#include "../inc/gameState.h"
void RenderHandler::render()
{
if (gameState() == State::start) {
// code
}
else if (gameState() == State::play) {
// code
}
else if (gameState() == State::stop) {
// code
}
}
BTW, you're not getting what you expect because the meaning of static
is different in the global scope. Read static vs global and What is external linkage and internal linkage?
Upvotes: 2
Reputation: 971
"static" does not have any meaning on global variables. You should mark global variable as "extern", and add its initialization to gamestate.cpp
Header: extern State gameState;
Cpp: State gameState = *initial enum value;
and add this cpp to compilation of your project.
Note: it will work as expected only in the same module, if you add gamestate.cpp to another module, it will create new global variable that is local for the module.
Upvotes: 0
Reputation: 5004
Your logic is wrong, if you declare a static variable in a header file and include it from two different C++ files, then what you are getting is two instances of a static variable with the same name but not using the same memory address.
In order to use one instance, you would either need to use a global variable (bad, because it makes the code hard to maintain), use a singleton (better, but still should be avoided because it is also hard to maintain), or simply create one instance of this state variable at the beginning of your program and then provide a reference to it in the classes using it in their constructors.
Upvotes: 0