Use static for three common jobs
Use static when a function or variable should be private to one file, keep its value between calls, or be shared by all objects of a class. Outside those cases, the keyword is usually not needed.
- Helpers used in one file. A function that only its own source file calls can be
static, which hides it from the rest of the program. - Values that must survive between calls. A
staticlocal variable is set up once and keeps its value. - Data shared by a whole class. A
staticdata member has one copy for all objects of the class.
Static member functions are a fourth case, covered below.
File-only helpers
Here is a helper that only its own file uses:
// text_tools.cpp
#include <string>
static bool is_blank(const std::string& s) { // only this file can call it
return s.find_first_not_of(" \t") == std::string::npos;
}
bool has_content(const std::string& s) {
return !is_blank(s);
}
An unnamed namespace does the same job, and many style guides prefer it for this purpose.
Values that must survive between calls
A static local variable is set up the first time execution reaches its declaration, and it keeps its value after the function returns:
#include <iostream>
int next_id() {
static int last_id = 0; // set up once, keeps its value between calls
return ++last_id;
}
int main() {
int first = next_id();
int second = next_id();
std::cout << first << ' ' << second << '\n'; // prints 1 2
}
This pattern suits counters and simple caches. Since C++11, the setup of a static local is thread-safe. Changing its value later is not automatically safe when several threads use it, so those changes still need protection.
Data shared by a whole class
A static data member is one variable shared by every object of the class:
#include <iostream>
class Widget {
public:
Widget() { ++count_; }
~Widget() { --count_; }
static int count() { return count_; }
private:
static int count_; // one copy, shared by all Widget objects
};
int Widget::count_ = 0; // the out-of-class definition is required
int main() {
Widget a;
Widget b;
std::cout << Widget::count() << '\n'; // prints 2
}
The out-of-class definition is required for a plain static data member. From C++17, you can write inline static int count_ = 0; inside the class instead.
Functions that don’t need an object
A static member function suits a helper that works only with its arguments. For how to write and call one, see Static Functions in C++.
C++ has no static class keyword the way C# does. A class that holds only static members is one common stand-in, which is covered in static classes in C++.
When to avoid static
- Mutable global state. Static data that many functions change is hard to follow and hard to test, because changes can come from many places.
- Global objects across files. Objects with static storage in different source files are set up in an order the language does not specify, so one can be used before it is ready.
- Values that never change. For those, the C++ const keyword states the intent more clearly than
staticdoes.
If a value only needs to live inside one function call, a local variable or a parameter is often clearer than a static one.