Advertisement Jump to content

Archived

This topic is now archived and is closed to further replies.

shakazed

Variable scope to other files

This topic is 6172 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

How do you make variables accesible from other .cpp files? Example: Hello.cpp int Hello = 10; //Initiating Hello in Hello.cpp Goodday.cpp int Goodday = Hello; //Initiating Gooday in Goodday.cpp sHaKaZeD

Share this post


Link to post
Share on other sites
Advertisement
By defining the variable in only one implementation file, and putting a declaration in each file that needs access to it.

This:

int x = 0;

Is a declaration and definition, whereas this:

extern int x;

Is a declaration only.

So, in short, write "extern int x" in each file that requires access to the variable, and the linker will sort it out.


Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!