Creating Global Variables For ALL Files. (C++)

Started by
5 comments, last by Zahlman 16 years, 2 months ago
Ether my brain is shutting down on me for not sleeping right now, but my mind is blank and I did some searching, but I want to create a variable that is defined and is available in each file to change the values for one file to another. Example if I had a bool called talk that I defined in one file and wanted to change the value in different CPP file or header how can I do it for all files?
Check out my open source code projects/libraries! My Homepage You may learn something.
Advertisement
you can use keyword extern as in:

=== main.cpp ===

float foo = 10;

=== other.cpp ===

extern float foo;
foo = 20;
OpenGL fanboy.
Of course, doing this is very bad programming practice, and should be avoided.
Quote:Original post by ajm113
Ether my brain is shutting down on me for not sleeping right now


If you are sleep deprived and can't figure something out, it is nearly always correct to sleep and worry about it later.

Quote:, but my mind is blank and I did some searching, but I want to create a variable that is defined and is available in each file to change the values for one file to another.


Why do you think you want to do this?
Thanks, got it going!

@Zahlman
Because I want to disable model rotation to allow the camera to rotate with out the model moving with it.

Check out my open source code projects/libraries! My Homepage You may learn something.
Argh. This is not a good enough reason to warrant globals IMHO. Think about if your code becomes more complex -- you'll sometimes want to look where you declared something and finding globals is such a pain :( (especially if you didn't write the code in the first place).

Just my two cents.

~Shiny
------------'C makes it easy to shoot yourself in the foot. C++ makes it harder, but when you do, it blows away your whole leg.' -Bjarne Stroustrup
Quote:Original post by ajm113
Thanks, got it going!

@Zahlman
Because I want to disable model rotation to allow the camera to rotate with out the model moving with it.


So, you have one module that represents the model, and offers an interface for rotating it, and another module that decides when/whether to rotate the model (and/or camera), and which can be told "never rotate the model" with a function, not a "published" global variable. Even if it needs a global for the implementation to work, you can put a saner interface on it.

This topic is closed to new replies.

Advertisement