Static variables exist during the "lifetime" of the translation unit in which they are declared, and:
It cannot be accessible from any other translation unit if it is in a namespace scope (i.e. outside of functions and classes).
This is referred to as "internal linking" or "static storage lifetime."
(Except for constexpr, do not do this in headers; otherwise, you would wind up with a different variable in each translation unit, which is really confusing.)
If it is a variable in a function, it, like any other local variable, cannot be accessed from outside the function.
(This is the mentioned local)
Class members have no limited scope owing to static, but they may be referenced from both the class and an instance (like std::string::npos).
locations as code:
static std::string namespaceScope = "Hello";
void ...READ MORE
Jun 27, 2022
in C++
by
Damon
• 4,960 points
•
536 views