Previously, we've talked about how classes are blueprints for creating objects. These objects, or instances, will all have the same structure. They have the same variables and the same functions that can access and update those variables.
Each object gets a copy of the class variables. As such, different objects can have different values for each variable:
#include <string>
class Vampire {
public:
int Health{100};
};
int main(){
Vampire A;
A.Health = 50;
Vampire B;
B.Health = 200;
}
In the above example, all instances of Vampire
will have a Health
value, and those values can be different for each object.
However, what if we want all objects in our class to share a variable? For example, all Vampires will have the same Faction
- we don’t need to have every object getting a separate copy of this std::string
:
#include <string>
class Vampire {
public:
std::string Faction{"Undead"};
};
In this lesson, we’ll introduce the concept of static members, which achieves exactly this.
Whilst a variable like Health
can vary from object to object, all of the objects of this class should share the same Faction
We can do this by marking the Faction
variable as static
and inline
:
#include <string>
class Vampire {
public:
int Health{100};
static inline std::string Faction{"Undead"};
};
inline
?C++ assumes a class definition is going to be included in multiple files. This is a reasonable assumption as, in a real program, it’s almost always true.
Static class variables are part of the class, rather than as part of objects of that class. Therefore, when a class declaration defines a static variable, that is very similar to defining a global variable. Static class members like the Vampire::Faction
symbol will have external/global linkage. We covered linkage in more detail in the previous lesson
Once a header containing a static class variable definition is included in multiple other files, each of those files would be defining that symbol.
This breaches the one definition rule (ODR), so the compiler pre-emptively asks us to mark in-class static declarations as inline
.
For variables, keeping the definition and declaration in the same place and marking it as inline
is generally recommended. But, as an alternative, we can move the definition to an implementation file file. Assuming we’re not going to be using the #include
directive to insert implementation files, this removes the need for inline
:
// Vampire.h
#pragma once
#include <string>
class Vampire {
public:
// Declaration
static std::string Faction;
};
// Vampire.cpp
#include "Vampire.h"
// Definition
std::string Vampire::Faction{"Undead"};
Static variables are shared across all instances of a class. In other words, any time we access the Faction
property of one of our Vampire
objects, we are accessing the same location in memory.
We can verify this by checking what is returned by &
, the address of operator:
#include <iostream>
#include <string>
class Vampire {
public:
static inline std::string Faction{"Undead"};
};
int main(){
Vampire A;
Vampire B;
if (&A.Faction == &B.Faction) {
std::cout << "They're the same";
}
}
They're the same
Given that all instances of a class share the same static variable, updating it on one object will have the effect of updating it on all objects of that class:
#include <iostream>
#include <string>
class Vampire {
public:
static inline std::string Faction{"Undead"};
};
int main(){
Vampire A;
Vampire B;
// Setting A.Faction
A.Faction = "Demonic";
// Getting B.Faction
std::cout << B.Faction;
}
Demonic
We can prevent a static variable from being updated in the usual ways, by marking it as a constant:
#include <string>
class Vampire {
public:
const static inline std::string Faction{
"Undead"};
};
int main(){
Vampire A;
// Cannot update a const
A.Faction = "Demonic";
}
error C2678: binary '=': no operator found which takes a left-hand operand of type 'const std::string'
constexpr
Static VariablesAnother implication of a class variable being static is that it can also be marked as a compile-time constant, using the constexpr
keyword.
A constexpr
variable is implicitly inline
, so we can simultaneously remove the inline
specifier:
class Vampire {
public:
static constexpr char Faction[] {"Undead"};
};
Note that as of C++23, a std::string
cannot be used as a constexpr
in this way. Therefore, we’ve also changed the type to a C-style string. We cover strings in more detail later in the course.
constexpr
VariablesIt might seem like Faction
could have been a compile-time constant even before it was marked as static
. After all, the value of "Undead"
was known at compile time.
However, before we marked it as static
, the variable itself did not exist at compile time. Because it was a regular class variable, it is only created when objects of that class are created. For this class, that can only happen at run time.
As such, a non-static member cannot be a constexpr
:
class Vampire {
public:
constexpr char Faction[] {"Undead"};
};
error C2178: 'Vampire::Faction' cannot be declared with 'constexpr' specifier
However, we can consider static variables as being part of the class, rather than part of the object. The class is known at compile time, so static class variables can be constexpr
.
Aside from these memory savings and the fact that our code now makes our intentions explicit, there is a further benefit to using static
when appropriate.
Due to the fact every static class member is shared across all instances of a class, we can find out what that value is without needing to create an object.
We can access a static class member directly from the class, using the scope resolution operator ::
#include <iostream>
#include <string>
class Vampire {
public:
static inline std::string Faction{"Undead"};
};
int main(){
std::cout << "All Vampires are " <<
Vampire::Faction;
Vampire::Faction = "Demonic";
std::cout << "\nThey're now " <<
Vampire::Faction;
}
All Vampires are Undead
They're now Demonic
static
FunctionsWe've seen how classes can have const
functions, which do not write to the properties of the object on which those functions are called.
We can also have static
functions. Static functions do not read nor write to the properties of the object, unless those properties are also static.
#include <string>
class Vampire {
public:
static std::string GetFaction(){
return Faction;
}
private:
static inline std::string Faction{"Undead"};
};
Static class functions can call other static functions. Attempting to call a non-static class function from a static class function will result in a compilation error.
Similar to static variables, static functions can be called from an object of the class using the .
operator.
But they can also be called from the class itself, using the ::
operator:
#include <iostream>
#include <string>
class Vampire {
public:
static std::string GetFaction(){
return Faction;
}
private:
static inline std::string Faction{"Undead"};
};
int main(){
std::cout << "All Vampires are "
<< Vampire::GetFaction();
}
All Vampires are Undead
Accessing static class members through the ::
operator bears some similarities to how we access members of a namespace.
To an extent, static members of a class can accomplish similar goals as namespaces, and the concept of a "static class" (a class that contains only static members) is a common pattern in other programming languages.
However, it tends to be less useful in C++. Namespaces achieve the goal in a simpler, more direct way. For the most part, we should only create classes (or structs) if we plan to instantiate objects from them.
But, sometimes a class is the most intuitive place to put a variable or a function, even if won’t change from object to object. So, for those scenarios, we can just add the code to our class, and mark it as static
.
In this lesson, we explored static class members, demonstrating how they allow for shared values and behaviors across instances
static
keyword is used to declare static class membersinline
with static variables in header files helps avoid multiple definitions across different translation units.::
without needing to instantiate an object.constexpr
with static members enables the definition of compile-time constantsA guide to sharing values between objects using static class variables and functions
Comprehensive course covering advanced concepts, and how to use them on large-scale projects.