Click here to Skip to main content
16,016,669 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
we are developing code in visual studio how can i access static variable from one file to another file
Posted

You don't. The point of static (or as is usually used these days, variables in anonymous namespaces) is to make a variable accessible from a single translation unit. So if someone's decorated a variable as static then their design either had a flaw in it (they didn't realise the information would be needed somewhere else) or they really don't want you fiddling about with the contents of that variable.

If you really need access to the static then you can remove the static decoration to make it a global and declare it as extern in the other translation unit. However make sure you know what you're doing as there might be a good reason for making this data static - two example could be it's not thread safe or they intend to move the lump of data somewhere else.
 
Share this answer
 
This is normally done using included header file and an "extern" declaration in it. In one of the file, you should also define this variable.
This is explained, for example, here: http://www.geeksforgeeks.org/understanding-extern-keyword-in-c[^].

You need to understand the background of separate compilation; what the compiler does and what the linker does.

With C++, it would be a good idea to have everything in classes (or enumeration types). To certain extent, this is a matter of personal taste.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900