Click to See Complete Forum and Search --> : C++ Global Vars help


enigma_0Z
February 25th, 2007, 11:30 AM
OK, I want to make an instance of a class global for an entire multi-file project... If it helps, it is an instance of a game class for a game. How do I do this?

Right now, I've got a file called "headers.h" which declares the actual class as so:

static CardGame game(640, 480, 32);

And files that need this variable "game" include headers.h ...

However, I'm convinced that this is somewhat improper, because I'm getting two instances of CardGame that are both named "game".

For some reason, this does not generate a compiler error, but I have proved that there's more than one floating around by putting an output statement into the CardGame constructor...

Anyhoo, should I declare that in main instead? I want this variable to have scope throughout the entire project.

SuperKoko
February 25th, 2007, 12:22 PM
However, I'm convinced that this is somewhat improper, because I'm getting two instances of CardGame that are both named "game".

Yes.
Here, static means "internal linkage".
In other words, there is one private CardGame variable for every module that includes the header.

In order to share a variable between several modules (external linkage variable), you must declare the variable in every translation unit which uses it and define the variable exactly once (one definition rule).

Header:

extern CardGame game; /* declaration */


One .cpp file:

#include "the_header.h"
CardGame game(640, 480, 32); /* definition */

This .cpp file may be, for example, a new .cpp file containing this single definition.
Or, it may be the .cpp file of the class definition, but that may make less sense.

enigma_0Z
February 25th, 2007, 12:34 PM
Yes.
Here, static means "internal linkage".
In other words, there is one private CardGame variable for every module that includes the header.

In order to share a variable between several modules (external linkage variable), you must declare the variable in every translation unit which uses it and define the variable exactly once (one definition rule).

Header:

extern CardGame game; /* declaration */


One .cpp file:

#include "the_header.h"
CardGame game(640, 480, 32); /* definition */

This .cpp file may be, for example, a new .cpp file containing this single definition.
Or, it may be the .cpp file of the class definition, but that may make less sense.

Thanks.

Another question however:

In the_header.h, do I need to wrap my stuff in an #ifndef/endif pair? such as:

/* the_header.h */
#ifndef GLOBAL_VARS
#define GLOBAL_VARS

extern CardGame game;

#endif

SuperKoko
February 25th, 2007, 05:28 PM
In the_header.h, do I need to wrap my stuff in an #ifndef/endif pair? such as:


You don't need to, but that's a pretty good idea. It will avoid multiple inclusion of the same header in the same translation unit, as may happen if you define a second header including the first header...
For example:

/* the_header.h */
extern CardGame game;


/* another_header.h */
#include "the_header.h"
/* a few other declarations */


/* a_file.cpp */
#include "another_header.h"
#include "the_header.h" /* inadvertently includes the_header for a second time */

Nevertheless, in that very simple example, including twice this header is not harmful as declaring an identifier twice is allowed.

i.e.:

extern CardGame game;
extern CardGame game; /* ok, no conflict */


For more complex headers, multiple inclusion may be harmful.
That's why, header protection is a usually useful.