Static (C++) 

When modifying a variable, the static keyword specifies that the variable has static duration (it is allocated when the program begins and deallocated when the program ends) and initializes it to 0 unless another value is specified. When modifying a variable or function at file scope, the static keyword specifies that the variable or function has internal linkage (its name is not visible from outside the file in which it is declared).

A variable declared static in a function retains its state between calls to that function.

When modifying a data member in a class declaration, the static keyword specifies that one copy of the member is shared by all instances of the class. When modifying a member function in a class declaration, the static keyword specifies that the function accesses only static members.

Static data members of classes must be initialized at file scope.

In recursive code, a static object or variable is guaranteed to have the same state in different instances of a block of code.

The members of a union cannot be declared as static. An anonymous union declared globally must be explicitly declared static.

Objects and variables defined outside all blocks have static lifetime and external linkage by default. A global object or variable that is explicitly declared as static has internal linkage.

For related information, see auto, extern, and register.

Example

The following example shows how a variable declared static in a function retains its state between calls to that function.

// static1.cpp
// compile with: /EHsc
#include <iostream>

using namespace std;
void showstat( int curr ) {
   static int nStatic;    // Value of nStatic is retained
                          // between each function call
   nStatic += curr;
   cout << "nStatic is " << nStatic << endl;
}

int main() {
   for ( int i = 0; i < 5; i++ )
      showstat( i );
}

Output

nStatic is 0
nStatic is 1
nStatic is 3
nStatic is 6
nStatic is 10

The following example shows the use of static in a class.

// static2.cpp
// compile with: /EHsc
#include <iostream>

using namespace std;
class CMyClass {
public:
   static int m_i;
};

int CMyClass::m_i = 0;

int main() {
   cout << CMyClass::m_i << endl;
   cout << CMyClass::m_i << endl;

   CMyClass::m_i = 1;
   cout << CMyClass::m_i << endl;
   cout << CMyClass::m_i << endl;
}

Output

0
0
1
1

The following example shows a local variable declared static in a member function. The static variable is available to the whole program; all instances of the type share the same copy of the static variable.

Note

Assigning to a static local variable is not thread safe and is not recommended as a programming practice.

// static3.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;
struct C {
   void Test(int value) {
      static int var = 0;
      if (var == value) 
         cout << "var == value" << endl;
      else
         cout << "var != value" << endl;

      var = value;
   }
}; 

int main() {
   C c1;
   C c2;
   c1.Test(100);
   c2.Test(100);
}

Output

var != value
var == value

See Also

Reference

C++ Keywords
C++ Storage Classes
Static Storage-Class Specifiers
Static Member Functions
Access Control and Static Members