Jump to content
  • Advertisement
Sign in to follow this  
willthiswork89

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.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;

Share this post


Link to post
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 variable
int Map[10][10];

// Tile.cpp or Tile.h
extern 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 this post


Link to post
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 this post


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

Share this post


Link to post
Share on other sites
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++

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!