C++ Reference ยท Types and Language Concepts
Static members of a class
Reference for C++ static class members: shared static data members, static member functions, in-class initialization with inline, and initialization order.
A static member belongs to the class itself instead of to each object. A static data member exists exactly once, no matter how many objects you create (even none). A static member function has no this pointer, so it can be called without an object and can only use other static members directly.
Use static members for state that is shared by all objects (an instance counter, a registry), for constants that belong to the type, and for factory or helper functions that conceptually belong to the class.
class Widget { public: static inline int count = 0; // one copy for the whole class static int total() { return count; } // no object needed };
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
class C { static int a; // declaration only: define in one .cpp file static inline int b = 0; // C++17: definition in the class static constexpr int c = 5; // implicitly inline since C++17 static void f(); // static member function }; int C::a = 0; // out-of-class definition (no 'static' here)
| Part | Description |
|---|---|
static data member |
Shared by all objects. A declaration inside the class is not a definition unless it is inline or constexpr. |
| Static member function | Called as C::f() or obj.f(). No this; cannot be const, virtual or volatile-qualified. |
| Access | Named with C::name. Access specifiers still apply. |
Usage notes
- Definition rule. Before C++17, a non-const static data member needed a definition in exactly one source file (
int C::a = 0;), or you got a linker error. Since C++17,inlinestatic members andconstexprstatic members are defined where they are declared. - Initialization order. Static members of a class in one translation unit are initialized in the order they are defined. Across translation units the order is unspecified (the "static initialization order fiasco"). Use
inline/constexpr, or a function-local static, to avoid depending on it. - A static member function cannot touch non-static members without an object. Pass the object in, or make the function non-static.
- Static members are not part of the object. They do not change
sizeofof the class. - Local classes cannot have static data members, and unnamed classes cannot either.
- Threads. Static data is shared by all threads. Counters like the one above need
std::atomic<int>if objects are created concurrently. - Static versus namespace-level variables. A static member gets class scope and access control; a namespace variable does not. Prefer the member when the data conceptually belongs to the type.
- Calling through an object (
c.max_users) is legal but misleading; writeConfig::max_users. - Destruction happens at program exit in reverse order of construction, so a destructor of another static object must not depend on a static member that is already gone.
- Unrelated meanings of
static: inside functions it means "keep the value between calls", at namespace scope it means "internal linkage". Seestatic.