Advertisement

global variables

Started by December 11, 2001 02:13 PM
12 comments, last by da_cobra 22 years, 11 months ago
quote: Header files ...get copied into every cpp file that includes them.

Not exactly or necessarily. It''s pretty close though. A header file should be parsed only once; at compile time all declarations become global, allowing for resolution of all references. This is why we use inclusion guards (#ifndef MY_HEADER_H...) or platform-specific instructions (#pragma once).

Which is more information than most people want or need.


[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
If you use those inclusion guards, you do not have to declare the variables as extern.

---
Make it work.
Make it right.
Make it fast.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
Advertisement
If you define a variable in a cpp file, use extern in other files to tell the compiler that it exists.

If you define a variable in a header file, make it include safe using this pattern:

#ifndef HEADERFILENAME_DEFINED
#define HEADERFILENAME_DEFINED

//here goes the usual header stuff

#endif

This way it will be included only once when compiling the whole project.
Kill mages first!
Don''t think anyone else posted about this.
if (bMenu=true) Menu() ; // go to the menu function
if (bGame=true) GameMain() ; // go to the game loop

should be:
if (bMenu==true) Menu() ; // go to the menu function
if (bGame==true) GameMain() ; // go to the game loop

or to prevent these problems from compiling:
if (bMenu) Menu() ; // go to the menu function
if (bGame) GameMain() ; // go to the game loop

or :
if (true == bMenu) Menu() ; // go to the menu function
if (true == bGame) GameMain() ; // go to the game loop

if you don''t understand this, you need to read more about how operators work in C/C++. There a lot more complex then what they look at times.


Code comment of the week:
bool hack = false; // DO NOT CHANGE THIS EVER
- Kevin "BaShildy" KingGame Programmer: DigiPenwww.mpogd.com

This topic is closed to new replies.

Advertisement