• Advertisement

Archived

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

Variable scope to other files

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