Why does a global variable define a static variable reset?

My code is very large, but I will try to minimize it as best as possible.

Basically, I want to define a line that has been changed in only one place (my main), and then read in my program.

My .h definitions are included everywhere, so I defined there.

static std::string MAINLOG = "RANDOMNES"; 

In my main function, I:

 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 listing my file

 Before CORRECT_VALUE RANDOMNESS After CORRECT_VALUE 

So my question is why and how can I get a value that will be supported inside my classes.

+5
source share
4 answers

Each translation unit (.c or .cpp file, basically) that includes defines.h will have its own copy of the variable.

I believe declare global extern in title

 extern std::string MAINLOG; 

and then defining it as a non-static global variable in any of the .c or .cpp files

 std::string MAINLOG = "RANDOMNES"; 

will solve the problem. But this is a bad coding style, IMO. C ++ - a path would be at least a singleton class.

I cannot give meaningful names without knowing the context, but the idea is this:

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 (do not put this in the header!)

 Mainlog& Mainlog::instance() { static Mainlog mainlog; return mainlog; } 
+6
source

Here is what I would recommend.

In define.h:

 const std::string& mainlog(); 

In main.cpp:

 const std::string& mainlog() { static std::string MAINLOG = "CORRECT_VALUE"; return MAINLOG; } 
+3
source

Since you put it in a define.h file, which you then include in your .cpp files, each .cpp file gets its own copy of the line, visible only inside that .cpp file. static makes the variable visible only inside .cpp where it is declared.

Change the static value to extern in your define.h, for example:

extern std::string MAINLOG;

Then, in one and only one of your .cpp files, add the following:

std :: string MAINLOG = "RANDOMNES";

This will give you the behavior you expect, but global variables like this are the idea of ​​BAD.

+1
source

When you define a variable with static storage in the header, each translation gets its own unique variable by that name.

A simple fix if you do not want to use a singleton path is to declare

 extern const std::string& MAINLOG; 

in your header and then the main file defines

 std::string MAINLOG_INTERNAL = "RANDOMNESS"; const std::string& MAINLOG = MAINLOG_INTERNAL; 

This will give you a writable string that has a "read" read-only in the rest of the program.

Then you can

 void prepDbugDir() { MAINLOG_INTERNAL = "CORRECTNESS"; } 

in the main file.

0
source

Source: https://habr.com/ru/post/1257848/


All Articles