Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable between two source files (class & global)

Tags:

c++

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?)

like image 859
Laurynas Lazauskas Avatar asked Jun 21 '15 18:06

Laurynas Lazauskas


People also ask

How do I use extern to share variables between source files?

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.

Are global variables shared between parent and child?

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.

How is a variable accessed from another file in C?

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 .


2 Answers

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.

like image 196
Ami Tavory Avatar answered Oct 02 '22 13:10

Ami Tavory


  1. 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.

  2. 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 #included. And cannot be accessed from elsewhere.

  3. static in OOP's is completely different from static in non-OOP's.

like image 39
Aditya Singh Avatar answered Oct 02 '22 14:10

Aditya Singh