I am building a project in Visual Studio 2008 in C++ where I have a couple of header and source code files. I would like to ask for your experience and knowledge regarding the possibility oif a project scope variable in C++. In other words, I would like the change a variable (lets say bool var) in A.cpp and have the changes reflect in B.cpp.
Thus if in A.cpp, var changes from 0 to 1, then in B.cpp the code reads var as 1 too.
Is it possible without setting up of events and interrupts?
(I could provide more details of what I am doing if needed. I havent done so, to keep the question simple)
Thank you for all the help in advance
Regards
Klen
yes, you define the variable in file A.cpp say and declare it extern in file B.cpp
conisder test.cpp which defines (allocates memory for variable) test
Code:
#include "test.h"
bool test; // define test
void changeTest(void)
{
test =true;
}
and associated header file test.h which declares (says it is defined somewhere) test
Code:
extern bool test; // declare variable test
void changeTest(void); // prototype for function
and main program which uses a variable test defined in test.cpp
Code:
#include <iostream>
using namespace std;
#include "test.h"
int _tmain(int argc, _TCHAR* argv[])
{
test = false;
cout << test << endl;
changeTest();
cout << test << endl;;
return 0;
}
when run prints
Code:
0
1
showing variable test changes from false 0 to true 1