In C++17 you can use inline variables, which you can use even outside classes.
The inline specifier, when used in a decl-specifier-seq of a variable with static storage duration (static class member or namespace-scope variable), declares the variable to be an inline variable.
A static member variable (but not a namespace-scope variable) declared constexpr is implicitly an inline variable.⁽¹⁾
For example:
class Someclass { public: inline static int someVar = 1; };
Or,
namespace SomeNamespace { inline static int someVar = 1; }
Expanded to answer Casey Kuball’s question:
What does inline do for variables?
Before C++17, if you defined a global variable in a header, including that header in multiple translation units would violate the One Definition Rule (ODR). You had to provide exactly one definition in a .cpp file, and only declarations elsewhere.
C++17’s inline variables were introduced specifically to lift this restriction.
When you mark a variable as inline, the compiler is allowed — and required — to treat all identical definitions of that variable across translation units as the same definition. In other words:
You may put the definition in a header.
Every translation unit may see (and compile) that definition.
The linker merges them into a single object with static storage duration.
This is the same ODR-forgiving mechanism already used for inline functions since C++98.
Does it create only one object?
Yes. An inline variable still has static storage duration. The program contains exactly one instance, just as with a normal global or static data member.
The standard stipulates that all definitions of an inline variable across translation units are merged into a single entity. Thus, a shared header definition does not produce multiple objects.
Is it initialized only once?
Yes. Initialization follows the usual rules for variables with static storage duration. It is performed exactly once, before main() begins (more precisely, as part of static initialization and dynamic initialization). Thus, across all translation units, the merged object is initialized only once. This matches what happens with a “regular” global variable defined in a single .cpp file, except you no longer need that .cpp file.
⁽¹⁾ https://en.cppreference.com/w/cpp/language/inline