Namespaces
Variants
Views
Actions

static specifier

From cppreference.com
 
 
C++ language
General topics
Preprocessor
Comments
Keywords
ASCII chart
Escape sequences
History of C++
Flow control
Conditional execution statements
Iteration statements
Jump statements
Functions
function declaration
lambda function declaration
function template
inline specifier
exception specifications (deprecated)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
decltype specifier (C++11)
Specifiers
cv specifiers
storage duration specifiers
constexpr specifier (C++11)
auto specifier (C++11)
alignas specifier (C++11)
Literals
Expressions
alternative representations
Utilities
Types
typedef declaration
type alias declaration (C++11)
attributes (C++11)
Casts
implicit conversions
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-style and functional cast
Memory allocation
Classes
Class-specific function properties
explicit (C++11)
static
Special member functions
Templates
class template
function template
template specialization
parameter packs (C++11)
Miscellaneous
Inline assembly
 

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

Contents

[edit] Syntax

static class_attribute
static class_method

[edit] Notes

  1. static attributes must be defined outside the class
  2. static constant integral attributes can be defined inline (until C++11)
  3. inside static methods there is no this pointer
  4. static methods cannot have other class-specific function specifier as cv, virtual or explicit overrides

[edit] See also

[edit] Example

struct C
{
    static void foo();
    static int n;
    static const int m = 16; // inline inizialization
};
 
int C::n = 0; // static attribute definition
 
void C::foo()
{
    // no reference to non-static members/methods can occur here
}