static
membersGeneral topics | ||||||||||||||||
Flow control | ||||||||||||||||
Conditional execution statements | ||||||||||||||||
Iteration statements (loops) | ||||||||||||||||
Jump statements | ||||||||||||||||
Functions | ||||||||||||||||
Function declaration | ||||||||||||||||
Lambda function expression | ||||||||||||||||
inline specifier | ||||||||||||||||
Dynamic exception specifications(until C++17*) | ||||||||||||||||
noexcept specifier(C++11) | ||||||||||||||||
Exceptions | ||||||||||||||||
Namespaces | ||||||||||||||||
Types | ||||||||||||||||
Specifiers | ||||||||||||||||
| ||||||||||||||||
Storage duration specifiers | ||||||||||||||||
Initialization | ||||||||||||||||
Expressions | |||||||||||||||||||||||||||||||
Alternative representations | |||||||||||||||||||||||||||||||
Literals | |||||||||||||||||||||||||||||||
Boolean -Integer -Floating-point | |||||||||||||||||||||||||||||||
Character -String -nullptr(C++11) | |||||||||||||||||||||||||||||||
User-defined(C++11) | |||||||||||||||||||||||||||||||
Utilities | |||||||||||||||||||||||||||||||
Attributes(C++11) | |||||||||||||||||||||||||||||||
Types | |||||||||||||||||||||||||||||||
typedef declaration | |||||||||||||||||||||||||||||||
Type alias declaration(C++11) | |||||||||||||||||||||||||||||||
Casts | |||||||||||||||||||||||||||||||
Memory allocation | |||||||||||||||||||||||||||||||
Classes | |||||||||||||||||||||||||||||||
Class-specific function properties | |||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||
Special member functions | |||||||||||||||||||||||||||||||
Templates | |||||||||||||||||||||||||||||||
Miscellaneous | |||||||||||||||||||||||||||||||
Inside a class definition, the keywordstatic declares members that are not bound to class instances.
Outside a class definition, it has a different meaning: seestorage duration.
Contents |
A declaration for a static member is amember declaration whose declaration specifiers contain the keywordstatic. The keywordstatic usually appears before other specifiers (which is why the syntax is often informally described asstaticdata-member orstaticmember-function), 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.
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)storage duration or regular functions.
Thestatic keyword is only used with the declaration of a static member, inside the class definition, but not with the definition of that static member:
class X{staticint n;};// declaration (uses 'static')int X::n=1;// definition (does not use 'static')
The declaration inside the class body is not a definition and may declare the member to be ofincomplete type (other thanvoid), including the type in which the member is declared:
struct Foo; struct S{staticint a[];// declaration, incomplete typestatic Foo x;// declaration, incomplete typestatic S s;// declaration, incomplete type (inside its own definition)}; int S::a[10];// definition, complete typestruct Foo{};Foo S::x;// definition, complete typeS S::s;// definition, complete type
However, if the declaration usesconstexpr orinline(since C++17) specifier, the member must be declared to have complete type. | (since C++11) |
To refer to a static memberm
of classT
, two forms may be used: qualified nameT::m
or member access expressionE.m
orE->m
, whereE
is an expression that evaluates toT
orT*
respectively. When in the same class scope, the qualification is unnecessary:
struct X{staticvoid f();// declarationstaticint n;// declaration}; X g(){return X();}// some function returning X void f(){ X::f();// X::f is a qualified name of static member function g().f();// g().f is member access expression referring to a static member function} int X::n=7;// definition void X::f()// definition{ n=1;// X::n is accessible as just n in this scope}
Static members obey theclass member access rules (private, protected, public).
Static member functions are not associated with any object. When called, they have nothis pointer.
Static member functions cannot bevirtual,const,volatile, orref-qualified.
The address of a static member function may be stored in a regularpointer to function, but not in apointer to member function.
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 staticstorage duration, unless the keywordthread_local is used, in which case there is one such object per thread with thread storage duration(since C++11).
Static data members cannot bemutable.
Static data members of a class in namespace scope haveexternal linkage if the class itself has external linkage (is not a member ofunnamed namespace). 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 declaredinline. 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: struct X{inlinestaticint fully_usable=1;// No out-of-class definition required, ODR-usableinlinestaticconststd::string class_name{"X"};// Likewise staticconstint non_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) |
If a static data member of integral or enumeration type is declaredconst (and notvolatile), it can be initialized with aninitializer in which every expression is aconstant expression, right inside the class definition:
struct X{conststaticint n=1;conststaticint m{2};// since C++11conststaticint k;};constint X::k=3;
If a static data member ofLiteralType is declaredconstexpr, it must be initialized with an initializer in which every expression is a constant expression, right inside the class definition: struct X{constexprstaticint arr[]={1,2,3};// OKconstexprstaticstd::complex<double> n={1,2};// OKconstexprstaticint k;// Error: constexpr static requires an initializer}; | (since C++11) |
If a constnon-inline(since C++17) static data memberor a constexpr static data member(since C++11)(until C++17) isODR-use, a definition at namespace scope is still required, but it cannot have an initializer.
Aconstexpr static data member is implicitlyinline 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) |
struct X{staticconstint n=1;staticconstexprint m=4;}; constint*p=&X::n,*q=&X::m;// X::n and X::m are ODR-usedconstint X::n;// … so a definition is necessaryconstexprint X::m;// … (except for X::m in C++17)
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
CWG 194 | C++98 | (static) member function names can be the same as the class name | naming restriction added (including non-static member functions) |