Alternative to global variables in C++
Use static variables and members of a class instead of global variables
When you need a variable to be readily available through your application code the most direct approach is to use a global variable. Unfortunately using global variables has some downsides.
Global variables couple the code components that rely on them to the specific context of the particular implementation. This pretty much makes the code useless outside of the context in which it was first written. For example, if you create a class that uses an external global variable it can only be used in the context where that variable was correctly defined and initialised. You can see that using global variables quickly becomes unmanageable and is not recommended.
Fortunately there is a better alternative to global variables, you can use a static variable or member of a class.
Static variable as part of a class
A static variable, or member, of a class is part of the class but not of an object of that class. There is just one copy of a static variable, or member, instead of one copy per object, as for ordinary class non-static variables or members.
This behaviour of the static variable of a class makes it possible to have the good parts of a global variable without the disadvantages. Just include the header file with the declaration of the class in the source code file where you need to access a global functionality (the “global” static variable of the class) and access it as you would it any other class variable.
Having a static variable as part of a class replace a global variable has several advantages like:
- Portable code
- Clear and visible declaration and definition, with documented interfaces
- Better maintainability of code
Advantages of static variable of a class over global variables
There is no coupling of your code to the overall code context. By including the class definition header your code has everything it needs.
By having the static variable being part of a class that is declared in a header file and implemented in a correspondent .cpp
source file everything is well encapsulated, structured and hopefully documented (or commented).
Also using a static variable as part of a class makes maintainability easier. When you are working in a source file you can easily refer to the header file with the class definition and you have not to guess where a global variable was declared and defined. It breaks the nebulous that global variable bring to code.
Example of usage of a static variable of a class
Declaration of the class with the static variable to be used instead of a global variable in the something.h
file.
#pragma once
class Something {
static int my_pseudo_global;
public:
static void set_global(int value);
static int get_global();
}
Class variables and members definition in the file something.cpp
.
#include "something.h"
int Something::my_pseudo_global = 0; // definition and initialisation
void Something::set_global(int value)
{
my_pseudo_global = value;
}
int Something::get_global()
{
return my_pseudo_global;
}
Now you include the something.h
header file in through out your code and be able to access and manipulate my_pseudo_global
variable global value.