Inside a class definition, the keyword
declares members that are not bound to class instances.
Outside a class definition, it has a different meaning: see
.
Syntax
A declaration for a static member is a
whose declaration specifiers contain the keyword static. The keyword static usually appears before other specifiers (which is why the syntax is often informally described as staticmember-declaration), but may appear anywhere in the specifier sequence.
The name of any static data member and static member function must be different from the name of the containing class.
Explanation
Static members of a class are not associated with the objects of the class: they are independent variables with static or thread(since C++11)
or regular functions.
The static keyword is only used with the declaration of a static member, inside the class definition, but not with the definition of that static member:
classX{staticintn;};// declaration (uses 'static')intX::n=1;// definition (does not use 'static')The declaration inside the class body is not a definition and may declare the member to be of
(other than void), including the type in which the member is declared:
structFoo;structS{staticinta[];// declaration, incomplete typestaticFoox;// declaration, incomplete typestaticSs;// declaration, incomplete type (inside its own definition)};intS::a[10];// definition, complete typestructFoo{};FooS::x;// definition, complete typeSS::s;// definition, complete typeHowever, if the declaration uses
or
(since C++17) specifier, the member must be declared to have complete type.
(since C++11)To refer to a static member m of class T, two forms may be used: qualified name T::m or member access expression E.m or E->m, where E is an expression that evaluates to T or T* respectively. When in the same class scope, the qualification is unnecessary:
structX{staticvoidf();// declarationstaticintn;// declaration};Xg(){returnX();}// some function returning Xvoidf(){X::f();// X::f is a qualified name of static member functiong().f();// g().f is member access expression referring to a static member function}intX::n=7;// definitionvoidX::f()// definition{n=1;// X::n is accessible as just n in this scope}Static members obey the
class member access rules (private, protected, public)
.
Static member functions
Static member functions are not associated with any object. When called, they have no this pointer.
Static member functions cannot be virtual, const, volatile, or
.
The address of a static member function may be stored in a regular
, but not in a
.
Static data members
Static data members are not associated with any object. They exist even if no objects of the class have been defined. There is only one instance of the static data member in the entire program with static
, unless the keyword
is used, in which case there is one such object per thread with thread storage duration(since C++11).
Static data members cannot be mutable.
Static data members of a class in namespace scope have
if the class itself has external linkage (is not a member of
). Local classes (classes defined inside functions) and unnamed classes, including member classes of unnamed classes, cannot have static data members.
A static data member may be declared
. An inline static data member can be defined in the class definition and may specify an initializer. It does not need an out-of-class definition:
structX{inlinestaticintfully_usable=1;// No out-of-class definition required,// ODR-usableinlinestaticconststd::stringclass_name{"X"};// Likewisestaticconstintnon_addressable=1;// C.f. non-inline constants, usable// for its value, but not ODR-usable// static const std::string class_name{"X"}; // Non-integral declaration of this// form is disallowed entirely};(since C++17)Constant static members
If a static data member of integral or enumeration type is declared const (and not volatile), it can be initialized with an
in which every expression is a
, right inside the class definition:
structX{conststaticintn=1;conststaticintm{2};// since C++11conststaticintk;};constintX::k=3;If a static data member of
is declared constexpr, it must be initialized with an initializer in which every expression is a constant expression, right inside the class definition:
structX{constexprstaticintarr[]={1,2,3};// OKconstexprstaticstd::complex<double>n={1,2};// OKconstexprstaticintk;// Error: constexpr static requires an initializer};(since C++11)If a const non-inline(since C++17) static data member or a constexpr static data member(since C++11)(until C++17) is
, a definition at namespace scope is still required, but it cannot have an initializer.
A constexpr static data member is implicitly inline and does not need to be redeclared at namespace scope. This redeclaration without an initializer (formerly required) is still permitted, but is deprecated.
(since C++17)structX{staticconstintn=1;staticconstexprintm=4;};constint*p=&X::n,*q=&X::m;// X::n and X::m are ODR-usedconstintX::n;// … so a definition is necessaryconstexprintX::m;// … (except for X::m in C++17)Keywords
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior
C++98 (static) member function names can be the same as the class name naming restriction added (including
) References
C++23 standard (ISO/IEC 14882:2024):
11.4.9 Static members [class.static]
C++20 standard (ISO/IEC 14882:2020):
11.4.8 Static members [class.static]
C++17 standard (ISO/IEC 14882:2017):
12.2.3 Static members [class.static]
C++14 standard (ISO/IEC 14882:2014):
9.4 Static members [class.static]
C++11 standard (ISO/IEC 14882:2011):
9.4 Static members [class.static]
C++98 standard (ISO/IEC 14882:1998):
9.4 Static members [class.static]
See also