Public Group

Accessing variables from one file to the other?

This topic is 4624 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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??

Share on other sites
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;

Share on other sites
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.

Share on other sites
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

Share on other sites
Quote:
 Original post by VasialekHello, 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

Share on other sites
Quote:
 Original post by Drew_BentonYes, there's a [slightly hackish, from what I've got as the general consensus] way to do this...
Quote:
 Original post by Kelly GDeclare 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++

Share on other sites
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.

1. 1
2. 2
3. 3
4. 4
frob
15
5. 5

• 16
• 12
• 20
• 12
• 13
• Forum Statistics

• Total Topics
632154
• Total Posts
3004472

×