Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

access to a variable from different files

Status
Not open for further replies.

markitus

Programmer
Apr 12, 2005
10
PL
hello... i know this is quite a beginner question. well, i explain my problem. i would like to access to one variable (p.e. int x) defined in a file (p.e. myprogramApp.cpp) from another file (p.e. myprogramView.cpp).. could anyone tell me how could i declare that variable, and how to get access to it from another file???
i hope it's possible.

thank u very much.
 
Method 1: put the variable in a header and declare it as extern. Include the header in both files. Then properly declare it in one of the source files.

Method 2: put the variable in a header and declare it as extern. Include the header in both files. Create a third file where the variable is declared.

Method 2 may not work on some compilers. The fussier older ones like some code in the source: they really moan if you just have declarations.
 
i like method 1 above.

also, if you are using classes and your var is a private member, you can use getters and setters.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top