What is the best way to declare a global variable?


Question

In C++, say you want to declare a global variable to be used by many. How do you do it?

I commonly use declare and define in cpp file, and then use extern in other cpp file (and not headers).

I don't like this approach, and I am considering something along these lines:

In a header file:

some_file.h

Class MYGlobalClass
{

};


MyGlobalClass& MyGlobalClassInstance()
{
   static MYGlobalClass instance; 
   return  instance;

}

Edit

Consider in the following contexts:

  • can be used in multi-threaded applications
  • namespace pollution
  • may NOT necessery be a singleton, as many instances of this might be created

What are your thoughts, suggestions, new ideas?

1
5
3/13/2009 3:38:18 PM

Accepted Answer

Declare it in one header file (using extern), and define it in one .cpp (or whatever other extension) file. You may use a function and return a reference to a static variable like you showed to circumvent problems with construction order relative to other such namespace scope variables in other .cpp files. But remember that won't protect you from destruction order problems - which is in the exact reverse order from construction (these things are called "static initialization order fiasco". If you use a function like yours and put it into headers, make it inline to make the redefinition of the function valid when it is included into multiple .cpp files (logically, the function is still only apparent once, because the static in it will only exist once, not separately for each file it is included into). Alternatively just declare it in a header but define it in one .cpp file (but then, remove the inline from it!).

inline A& getA() { static A a; return a; }

The potential problems with destruction order can be circumvented by using new:

inline A& getA() { static A *a = new A; return *a; }

The destructor of it, however, will never be called then. If you need thread safety, you should add a mutex that protects against multiple accesses. boost.thread probably has something for that.

2
3/11/2009 6:41:02 PM

The best advice is probably "try to avoid globals". People don't need global variables as often as they think. Usually it turns out that "passing everything as arguments to constructors" isn't quite as much work as people think when they hear the suggestion. It also tends to lead to cleaner code with fewer, and more explicit, dependencies.

I'm not aware of any "correct" way to declare globals in C++. The way you do it now works fine, but the order of initialization is unspecified, so if there are any dependencies between your globals, you're in trouble.

A function returning a static instance more or less solves that problem, but isn't thread safe.

And a singleton is just a terrible idea. It doesn't solve your problem, but adds additional constraints to your code, which weren't actually necessary, and will most likely come back and bite you later.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon