Jump to content
  • Advertisement
Sign in to follow this  
BloodLust666

extern global variables

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

I have an extern variable in one source, i redefine it in another and i use the global variable in a different source (all in the same project and i'm including the header of the redifined variable in the header where i use it) but for some reason it's acting as if it's a totally different instance of it.

Share this post


Link to post
Share on other sites
Advertisement
With extern variables you declare it once (usually in a header) and then use it through out the code...making sure that the header it is declared in is included in all modules that are going to use it.

Share this post


Link to post
Share on other sites
it is, here's an example. i declared
"extern cGraphics Graphics;"
in Graphics.h. i define it
"cGraphics Graphics;"
in Application.cpp
and i use it again in
"World.h" and in World.h i included "Graphics.h".

so i don't know what the problem is... everythign is right... i think..

Share this post


Link to post
Share on other sites
If Grahpics.h is the header that is to be included throughout the projects then declare it extern in there and don't re-define it...just use it.

for example if you declare an int:

int x;

you just use it straight away:

x = 1;

You don't redeclare it:

int x;
x = 1;

I think that your 2nd "definition" is in fact a redeclaration.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Well, if you did everything as you said, it should work, I have done it like that a million times. Check for some typos or something, cos' it should work, definatelly.

But anyway, you should look into Singletons - nice, short & to the point article here on GDnet, becouse it is considered bad practise to have globals in the first place, and I guess your problem is one of the cases why it is considered so.

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!