0

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.

2
  • Do you mean you want to access the variable varA in b.cpp after it has been modified in a.cpp? Commented May 25, 2012 at 6:25
  • Are these variables global or within a function? Commented May 25, 2012 at 6:32

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Declare variable in some header file and then include this header to cpp file where you want to use it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.