cpp/language/static

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. The keyword usually appears before other specifiers (which is why the syntax is often informally described as   or  ), 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 or regular functions.

The keyword is only used with the declaration of a static member, inside the class definition, but not with the definition of that static member:

The declaration inside the class body is not a definition and may declare the member to be of (other than ), including the type in which the member is declared:

To refer to a static member of class, two forms may be used: qualified name  or member access expression  or , where  is an expression that evaluates to  or  respectively. When in the same class scope, the qualification is unnecessary:

Static members obey the.

Static member functions
Static member functions are not associated with any object. When called, they have no pointer.

Static member functions cannot be, , , 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.

Static data members cannot be.

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.

Constant static members
If a static data member of integral or enumeration type is declared (and not ), it can be initialized with an  in which every expression is a, right inside the class definition:

If a const static data member  is, a definition at namespace scope is still required, but it cannot have an initializer.