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
- Static Local Variables: Variables defined as
staticinside a function retain their value between function calls. - Static Global Variables: When used in the global scope,
staticlimits the variable’s visibility to the file it is declared in (file-level scope). - Static Member Variables: A
staticmember variable of a class is shared across all objects of the class and is associated with the class, not with any specific object. - Static Member Functions: A
staticmember 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:
- The variable
countis declared static, so it retains its value between calls to thecounterfunction. - Each call increments the value of
countand 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:
- The static global variable
globalVaris only accessible within the file where it is declared. - 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:
- The static member variable
countis shared by all objects of the class and is initialized outside the class. - Static member functions can access only static variables and can be called without creating an object.
- In this program,
incrementanddisplayCountoperate on the sharedcountvariable.
Key Points to Remember about static Keyword
staticlocal variables retain their value between function calls.staticglobal variables have file-level scope and are not visible in other files.- Static member variables are shared across all instances of a class.
- Static member functions can be called without creating an instance of the class.
- Static variables must be initialized only once and persist for the lifetime of the program.
