Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] C++ : Project scope variable

Status
Not open for further replies.

Klen

Member level 1
Member level 1
Joined
Nov 15, 2010
Messages
36
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,288
Location
Germany
Activity points
1,602
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
 
  • Like
Reactions: Klen

    Klen

    Points: 2
    Helpful Answer Positive Rating
That exactly answers my question. Sometimes one skips the most baisc things! I didn't even think about extern!
Thank you so very much.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top