|
-
February 25th, 2007, 12:30 PM
#1
C++ Global Vars help
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:
Code:
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.
-
February 25th, 2007, 01:22 PM
#2
Re: C++ Global Vars help
 Originally Posted by enigma_0Z
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:
Code:
extern CardGame game; /* declaration */
One .cpp file:
Code:
#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.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
February 25th, 2007, 01:34 PM
#3
Re: C++ Global Vars help
 Originally Posted by SuperKoko
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:
Code:
extern CardGame game; /* declaration */
One .cpp file:
Code:
#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:
Code:
/* the_header.h */
#ifndef GLOBAL_VARS
#define GLOBAL_VARS
extern CardGame game;
#endif
-
February 25th, 2007, 06:28 PM
#4
Re: C++ Global Vars help
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:
Code:
/* the_header.h */
extern CardGame game;
Code:
/* another_header.h */
#include "the_header.h"
/* a few other declarations */
Code:
/* 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.:
Code:
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.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|