A static data member in C++ is a variable declared with static inside a class that belongs to the class itself rather than to any object. All objects share the same single copy, and it exists even if no object has been created. Use it for data that is common to the whole class, such as a counter of instances or a shared default.
This guide shows how to declare and define static data members, how to access them, and how modern C++ (C++17 inline variables) removed the most common source of linker errors.
Inside the class you only declare the member. Each ordinary data member is stored once per object, but a static one is stored once for the whole class.
Access it through the class name with the scope operator (Student::count_) rather than through an object, because that makes clear that the data is not per object.
Before C++17, a non-const static data member had to be defined exactly once outside the class, in one source file. Forgetting this gives a linker error such as undefined reference to Student::count_.
In C++17 and later, write static inline int count_ = 0; in the class and the separate definition is no longer needed, even in a header included by many files.
Static constants of integral or enumeration type can be initialised inside the class, and static constexpr works for any literal type. Since C++17, static constexpr members are implicitly inline.
Use static constexpr for compile-time constants, and static inline const for constants that need runtime construction like a std::string.
Static members obey public, protected and private like any other member. A common pattern is a private static member with public static functions to read or change it, as count() above does. Static member functions have no this pointer, so they can use static data members directly but cannot touch non-static members without an object.
| Property | Non-static member | Static member |
|---|---|---|
| Storage | one copy per object | one copy per class |
| Exists without an object | no | yes |
| Accessed with | object or this | Class::name or an object |
Used in sizeof(Class) | yes | no |
| Initialised by | constructor | its definition or initialiser |
Because a static member is not part of any object, sizeof(Student) does not include count_.
std::atomic<int> or protected by a mutex.Each Request gets a unique increasing id, and the atomic prevents two threads from receiving the same value.
Since C++17 you can use static inline in the class. Before that, define it once in a single .cpp file as Type Class::name = value;.
Yes. Use ClassName::member, subject to its access level.
You declared it in the class but never defined it, which is the pre-C++17 rule. Add the definition in one source file, or use inline.
Yes. A derived class sees the same single static member of the base class, not a separate copy.
No. It lives in static storage, outside every object.
A static data member is one variable shared by every object of a class and available before any object exists. Declare it static inline (C++17) or define it once in a source file, access it as Class::member, use std::atomic or a mutex when threads share it, and prefer static constexpr for constants.