C++ static Keyword

The static keyword in C++ has multiple uses, depending on where it is applied. It can be used with variables, functions, and class members, each serving a distinct purpose.

In this tutorial, we’ll explore the syntax and common uses of static keyword in C++.


Common Uses of static Keyword

  1. Static Local Variables: Variables defined as static inside a function retain their value between function calls.
  2. Static Global Variables: When used in the global scope, static limits the variable’s visibility to the file it is declared in (file-level scope).
  3. Static Member Variables: A static member variable of a class is shared across all objects of the class and is associated with the class, not with any specific object.
  4. Static Member Functions: A static member function of a class can be called without creating an object of the class and can only access other static members.

Syntax

</>
Copy
// Static local variable
static type variable_name;

// Static global variable
static type variable_name;

// Static member variable or function in a class
class ClassName {
    static type variable_name;
    static return_type function_name();
};

Examples

Example 1: Static Local Variable

A static local variable retains its value between function calls.

</>
Copy
#include <iostream>
using namespace std;

void counter() {
    static int count = 0; // Static local variable
    count++;
    cout << "Count: " << count << endl;
}

int main() {
    counter(); // Output: Count: 1
    counter(); // Output: Count: 2
    counter(); // Output: Count: 3
    return 0;
}

Output:

Count: 1
Count: 2
Count: 3

Explanation:

  1. The variable count is declared static, so it retains its value between calls to the counter function.
  2. Each call increments the value of count and prints it.

Example 2: Static Global Variable

Static global variables have file-level scope and are not visible in other files.

</>
Copy
#include <iostream>
using namespace std;

static int globalVar = 10; // Static global variable

void printGlobalVar() {
    cout << "GlobalVar: " << globalVar << endl;
}

int main() {
    printGlobalVar(); // Output: GlobalVar: 10
    return 0;
}

Output:

GlobalVar: 10

Explanation:

  1. The static global variable globalVar is only accessible within the file where it is declared.
  2. This helps encapsulate the variable and prevent conflicts with other global variables in different files.

Example 3: Static Member Variables and Functions

Static member variables and functions are shared across all objects of the class.

</>
Copy
#include <iostream>
using namespace std;

class Counter {
    static int count; // Static member variable
public:
    static void increment() { // Static member function
        count++;
    }
    static void displayCount() {
        cout << "Count: " << count << endl;
    }
};

// Define the static member variable
int Counter::count = 0;

int main() {
    Counter::increment();
    Counter::increment();
    Counter::displayCount(); // Output: Count: 2
    return 0;
}

Output:

Count: 2

Explanation:

  1. The static member variable count is shared by all objects of the class and is initialized outside the class.
  2. Static member functions can access only static variables and can be called without creating an object.
  3. In this program, increment and displayCount operate on the shared count variable.

Key Points to Remember about static Keyword

  1. static local variables retain their value between function calls.
  2. static global variables have file-level scope and are not visible in other files.
  3. Static member variables are shared across all instances of a class.
  4. Static member functions can be called without creating an instance of the class.
  5. Static variables must be initialized only once and persist for the lifetime of the program.