Accessing variables from one file to the other?

Started by
5 comments, last by Zahlman 18 years, 4 months ago
Im doing c++ and i dunno if ill need to but its a possibility...if there a way to access a global variable in lets say..Main from like Tile??
Advertisement
Declare the variable in your "Main" file again but put the keyword "extern" before the declaration. It tells the compiler that it's the same variable as the one declared in your other file.

like:

extern int x;
Yes, there's a [slightly hackish, from what I've got as the general consensus] way to do this. Basically you just redefine the variable again in the other file you want to use it, but prefix it with the keyword 'extern'. This tells the compiler that is will be declared somewhere else, but let the current module have access to it. Here's an example:
// Main.cpp// The original declaration of the variableint Map[10][10];// Tile.cpp or Tile.hextern int Map[10][10];

Or you could do it so the extern int Map[10][10]; falls in a header file, and just include that header file where you want to use the variable at.
Hello, this is my 5ct :) According to c++ coding standart - " Global variables should be prepended with a 'g'"

So if you want to use Map as global, better name it gMap That's all :)

Lekha
Quote:Original post by Vasialek
Hello, this is my 5ct :) According to c++ coding standart - " Global variables should be prepended with a 'g'"

So if you want to use Map as global, better name it gMap That's all :)

Lekha


That's...interesting. While it's something that is sometimes used by many people it is not part of any official c++ standard. The c++ standard only restricts the use of prefixed underscores on global variables.

Personally, I just leave it off. I don't like prefixed variables in any case - it only leads to hungarian notation
Quote:Original post by Drew_Benton
Yes, there's a [slightly hackish, from what I've got as the general consensus] way to do this...
Quote:Original post by Kelly G
Declare the variable in your "Main" file again but put the keyword "extern" before the declaration. It tells the compiler that it's the same variable as the one declared in your other file.

Don't do that. It will create major problems if you change the variable but you don't make the identical change everywhere it is declared with extern. Put it in a header file that is included wherever the variable is needed.

Read this: Organizing Code Files in C and C++
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
That is to say, don't put the externs in the other .cpp files that use the global, but instead put it in the .h that corresponds to the .cpp with the global. :) Then you can include the .h where you need access to the global, and everything is still reasonably well encapsulated.

However, it pays to consider why one is doing this. Experienced programmers may be able to help you find a better solution to the "communication" problem.

This topic is closed to new replies.

Advertisement