Reputation: 3810
I have a VC++ solution (using VS2008) with 2 projects Project A & project B.
Everything I discuss below is wrt Project A.
Project A has a sourec file a.cpp & it includes a header file "a.h". "a.h" has a variable int varA; which is modifies inside a.cpp.
a.cpp:
int varA = x*2;
// also do some calculations with varA in a.cpp
Now there is one more sourec file b.cpp in the same project A. And now lets say this "b.cpp" also includes same header file "a.h". i.e
b.cpp
int varB = varA; // If I want to access varA & get the current value of varA here in b.cpp what should do ? Will it work if I declare varA as extern in a.h & include
Here it's important to note that I wnat to not only access the variable varA from within b.cpp but also want to access the current value of this variable as updated by a.cpp.
Thnaks in advance.
Upvotes: 0
Views: 176
Reputation: 1563
Create a header file A.h
extern int varA;
In A.cpp declare variable
int varA;
Include "A.h" in b.cpp
That's it.
Upvotes: 2
Reputation: 2848
Declare variable in some header file and then include this header to cpp file where you want to use it.
Upvotes: 0