My code is very large, but I'll try to minimize it here as best as possible.
Basically I want to define a string that is modified in only one place (my main) and then be read in my entire program.
My defines.h is included everywhere so in there I defined.
static std::string MAINLOG = "RANDOMNES";
In my main function I do:
for (int i = 0; i < files.size(); i++){
// Do stuff
prepDbugDir(); // This sets MAINLOG to "CORRECT_VALUE"
std::cout << "Before " << MAINLOG << std::endl;
// Call a class function whose includes include my defines.h
std::cout << "After " << MAINLOG << std::endl;
}
And the print out of my file is
Before CORRECT_VALUE
RANDOMNESS
After CORRECT_VALUE
So my question is why and how can I get the value to be maintained inside my classes.
A global static variable is one that can only be accessed in the file where it is created. This variable is said to have file scope. In C, the preprocessor directive #define was used to create a variable with a constant value. This still works in C++, but problems could arise.
Global and static variables are initialized to their default values because it is in the C or C++ standards and it is free to assign a value by zero at compile time. Both static and global variable behave same to the generated object code.
There are similarities between a static variable in a function and a global variable. Both have a lifetime that is the same as that of the program as a whole. This means that any changes made to the static variable in the function are preserved between calls to the function.
A global variable can be accessed from anywhere inside the program while a static variable only has a block scope. So, the benefit of using a static variable as a global variable is that it can be accessed from anywhere inside the program since it is declared globally.
Every translation unit (.c or .cpp file, basically) that includes defines.h
will have its own copy of the variable.
I believe declaring the global extern
in the header
extern std::string MAINLOG;
and then defining it as a non-static global variable in any one of the .c or .cpp files
std::string MAINLOG = "RANDOMNES";
will solve the problem. But it's poor coding style, IMO. The C++ way would be at least a singleton class.
I can't give meaningful names without knowing the context, but the idea is as follows:
mainlog.h
class Mainlog {
Mainlog() = default; // Private default constructor
static Mainlog& instance();
public:
static const std::string& get() {
return instance().value;
}
static void set(const std::string& newValue) {
instance().value = newValue;
}
private:
std::string value {"RANDOMNESS"};
};
mainlog.cpp (don't put this in a header!)
Mainlog& Mainlog::instance() {
static Mainlog mainlog;
return mainlog;
}
Here's what I would recommend.
In defines.h:
const std::string& mainlog();
In main.cpp:
const std::string& mainlog() {
static std::string MAINLOG = "CORRECT_VALUE";
return MAINLOG;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With