Consider having one variable that is handled by one .cpp
file, while other .cpp
files use that variable's value for various purposes.
When used with classes
// header.h
class c {
public:
static int f1();
static int f2();
private:
static int v;
};
// first.cpp
int c::v(0);
int c::f1() { return ++v; }
// second.cpp
int c::f2() { return ++v; }
// main.cpp
int main() {
cout << c::f1() << endl;
cout << c::f2() << endl;
return 0;
}
Output is:
1
2
When used in global scope
// header.h
int f1();
int f2();
static int v = 0;
// first.cpp
int f1() { return ++v; }
// second.cpp
int f2() { return ++v; }
// main.cpp
int main() {
cout << f1() << endl;
cout << f2() << endl;
return 0;
}
Output is:
1
1
How come when said variable is in a class, the output is what you expect and otherwise it is not? (I am aware that using extern
in the second part would give the wanted result, the question is rather why static
works in case with the class but not with the global scope?)
The clean, reliable way to declare and define global variables is to use a header file to contain an extern declaration of the variable. The header is included by the one source file that defines the variable and by all the source files that reference the variable.
No, they are not shared in any way which is visible to the programmer; the processes can modify their own copies of the variables independently and they will change without any noticable effect on the other process(es) which are fork() parents, siblings or descendents.
The variable number is declared globally and may be accessed from other file when including its declaration with the “ extern ” prefix. However, the variable coefficient is only accessible from the file in which it is declared and only from that point on (it is not visible in function main .
static
has more than a single meaning in C++ (the language designers actually do this thing quite a lot, in order to reduce the number of reserved keywords).
static
in a class means that it is a variable shared by all instances of the class.
static
in a a compilation unit means that it cannot be addressed in a different compilation unit. In this case, you wrote static
in the header; the preprocessor (via the ``#include`s) inserted it into compilable source files. In each source file including this header, it simply means that this is a variable that is local to this compilation unit.
The keyword static
, when used in a class
, means the member of that class is shared by all it's instances. So every time v
is incremented in class c
, it returns a value greater than the previous value.
But when the keyword static
is used in global space, has nothing to do with objects. static
in global space means ONLY that variable v
is accessible from within that .cpp
file in which it is declared/defined and in those files in which it's file is #include
d. And cannot be accessed from elsewhere.
static
in OOP's is completely different from static
in non-OOP's.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With