C++ Global variable declaration

C++VariablesGlobal Variables

C++ Problem Overview



What I want to do is just to define a variable in a header file and use it on two different cpp files without redefinition that variable each time I include that header
Here is how I tried :

Variables.h

#ifndef VARIABLES_H // header guards
#define VARIABLES_H

static bool bShouldRegister;

#endif

(I also tried extern but nothing changed)

And in a cpp file I give it a value ::bShouldRegister = true or bShouldRegister = true;

In my another cpp file I check it's value by creating a thread and check its value in a loop (and yes my thread function works well)

 while (true)
 {
     if (::bShouldRegister) // Or if (bShouldRegister)
        {
            MessageBox(NULL,"Value Changed","Done",MB_OK|MB_ICONINFORMATION);
        }
  Sleep(100);
 }

And yes, that MessageBox never appears (bShouldRegister never gets true :/)

C++ Solutions


Solution 1 - C++

You must use extern, otherwise you will have separated bShouldRegister variables in each translation unit with probably different values.

Put this in a header file (.h):

extern bool bShouldRegister;

Put this in one of implementation files (.cpp):

bool bShouldRegister;

Solution 2 - C++

If you can use C++17, consider using an inline variable:

// in a header file
inline bool bShouldRegister = true;

See https://stackoverflow.com/questions/38043442/how-do-inline-variables-work for more information.

Solution 3 - C++

A more C++-like way would be using a class member, syntactically indicated by the static keyword. Class member variables have implicit external linkage.

#ifndef VARIABLES_H
#define VARIABLES_H

class RegUtil {
public:

    static bool bShouldRegister;

};

#endif

in one of your cpp files (maybe variables.cpp), you have to define this class member:

#include "variables.h"

bool RegUtil::bShouldRegister;

Solution 4 - C++

You need to define the variable in one of the modules:

bool bShouldRegister;

Then declare it extern (not static!) in the header:

extern bool bShouldRegister;

Solution 5 - C++

Here you need to define bool bShouldRegister in one class. Your header file should like this.

 #ifndef VARIABLES_H // header guards
#define VARIABLES_H

class abc{

public:
      bool bShouldRegister;
      abc();
#endif

Now initialize bShouldRegister variable in cpp file in constructor of abc class and then use this variable in your second cpp file using object of class. You will get your message box.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShahriyarView Question on Stackoverflow
Solution 1 - C++masoudView Answer on Stackoverflow
Solution 2 - C++bcdView Answer on Stackoverflow
Solution 3 - C++WolfView Answer on Stackoverflow
Solution 4 - C++Fred FooView Answer on Stackoverflow
Solution 5 - C++Jayesh VaghasiyaView Answer on Stackoverflow