Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/language/static"

From cppreference.com
< cpp‎ | language
m (grammar)
Line 72: Line 72:
 
Static member functions cannot be {{c|virtual}}, {{c|const}}, or {{c|volatile}}.
 
Static member functions cannot be {{c|virtual}}, {{c|const}}, or {{c|volatile}}.
  
Address of a static member function may be stored in a regular {{rlp|pointer#Pointers to functions|pointer to function}}, but not in a {{rlp|pointer#Pointers to member functions|pointer to member function}}.
+
The address of a static member function may be stored in a regular {{rlp|pointer#Pointers to functions|pointer to function}}, but not in a {{rlp|pointer#Pointers to member functions|pointer to member function}}.
  
 
====Static data members====
 
====Static data members====

Revision as of 05:43, 29 October 2015

 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
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
explicit (C++11)
static

Special member functions
Templates
Miscellaneous
 
 

Inside a class, declares members not bound to specific instances.

Contents

Syntax

static data_member (1)
static member_function (2)
1) Declares a static data member
2) Declares a static member function.

Explanation

Static members of a class are not associated with the objects of the class: they are independent objects with static storage duration or regular functions defined in namespace scope, only once in the program.

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:

class X { static int 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 of incomplete type (other than void), including the type in which the member is declared:

struct Foo;
struct S
{
   static int a[]; // declaration, incomplete type
   static Foo x;   // declaration, incomplete type
   static S s;     // declaration, incomplete type (inside its own definition)
};
 
int S::a[10]; // definition, complete type
struct Foo {};
Foo S::x;     // definition, complete type
S S::s;       // definition, complete type

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:

struct X
{
    static void f(); // declaration
    static int n;    // declaration
};
 
X g() { return X(); } // some function returning X
 
void f()
{
    X::f();  // qualified name access to static member function
    g().f(); // expression member access to 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 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, or volatile.

The address of a static member function may be stored in a regular pointer to function, but not in a pointer to member function.

Static data members

Static member objects are not associated with any object. They exist even if no objects of the class have been defined. If the static member is declared thread_local(since C++11), there is one such object per thread. Otherwise, there is only one instance of the static member object in the entire program, with static storage duration.

Static data members cannot be mutable.

Static data members of a class in namespace scope have external linkage if the class itself has external linkage (i.e. is not a member of unnamed namespace). 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 const (and not volatile), it can be initialized with a brace-or-equal initializer that is a constant expression, right inside the class definition:

struct X
{
    const static int n = 1;
    const static int m{2}; // since C++11
};

If a static data member of Template:concept is declared constexpr, it can be initialized with a brace-or-equal initializer that is a constant expression, right inside the class definition:

struct X { constexpr static int n = 1; };
(since C++11)

If such a member is odr-used, a definition at namespace scope is still required, but it should not have an initializer:

struct X { const static int n = 1; };
const int* p = &X::n; // X::n is odr-used
const int X::n;       // … so a definition is necessary

References

  • 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