My goal is to have global constants in a C++ game I'm working on (to represent some graphics info and the like). My current implementation is to toss them all in a .h and include them everywhere. This works, except that every time I change a setting, the entire code base must be recompiled.
So, my next idea was to toss them in some configuration txt file and parse them in, that way no code is actually changed when settings change. The parser was simple enough, and I could put the values into the constants, but because the parser was a code block, the constants were no longer global.
Is there a good way to solve this? Perhaps some way to make them global despite being in a block or some way to avoid recompiling everything when changing settings?
Another way to do this would be to create a singleton class.
Here the configuration is saved in a map, meaning as long as parseFile can read the file and getValue can parse the type there is no need to recompile the config class if you add new keys.
Usage:
The way I used solve this is to put the variables in a separate global namespace, which is in a header file named something like
config.h
, then include that file everywhere.The in a source file, you define the variable and also set them to a default value. This source file also have the code to load the variables from your configuration file.
Now in every source file you need the configuration variables, include
config.h
and access them likeconfig::some_config_int
.However, there is no "proper" way of solving this, all ways that work are proper in my eyes.
What about creating functions that return your constants that you can specify in a .cxx file? For example:
put only the declarations in head file and put the definitions in a cpp file. then you change the definitions in cpp file will not cause all code recompiled