Archived

This topic is now archived and is closed to further replies.

Trajar

A way around globals

Recommended Posts

I have about 10 variables (such as mouse pos., the map, screen offsets, etc.) that need to be accessed by about 3/4 of the objects in my game. I have played with an inherited static member class, but I keep getting ''unresolved linker'' errors. I put them all in a class and shoved them into memory, then passing the ptr to the ''data'' class throughout the program as needed, but this is cumbersome. Then I thought about extern variables, but I thought before I got too far, I would ask for a better way. So... How do you access a few variables the C++ way? Thanks in advance..

Share this post


Link to post
Share on other sites
Use the Singleton Design Pattern. For example, I develop using Windows, but I like to port my games to other OS's. So, I have an OSParams class that contains all OS specific variables that I might need to keep track of. Here's an example of my OSParams class definition.

//OSParams.h

class OSParams
{
//Class Attributes
public:
static OSParams* getInstance()
{
if( osParams == 0 )
{
osParams = new OSParams();
}
return osParams;
}
private:
static OSParams* osParams;

//Attributes
public:
HINSTANCE hInst;
HWND hWnd;
etc....

So, the real interesting part is "getInstance()" function. This is a class scope function that when called returns the class scope variable "osParams". If osParams has not been initialized (if osParams == 0), then it creates a new OSParams object. Now, in order to avoid linker errors, you have to also include in the .cpp file this line of code...

OSParams* OSParams::osParams = 0;

This initializes the class scope "osParams" variable to 0. This has to be done because it's declared as static. So, whenever I want to use the handle to my window "hWnd" I simply do something like this...

#include "OSParams.h"

...

OSParams* osp = OSParams::getInstance();
ShowWindow( osp->hWnd, SW_SHOWNORMAL );

Piece of cake. Hope this helps .

-Dave

P.S. If you've got alot of Singletons floating around, it can get very hard to manage their allocation and/or deallocation. If you want, you can implement the Object Life Manager Pattern to manage the life of you Singletons. A full discussion of this can be found in one of the back issues of C++ report. http://www.creport.com


Edited by - dmiles96 on July 23, 2000 3:55:48 AM

Share this post


Link to post
Share on other sites
If your going to be using singletons, you might find it easier to use a function static instead a static member variable. e.g.



class OSParams
{
//Class Attributes
public:
static OSParams& getInstance()
{
static OSParams instance;
return instance;
}
//...
}



Doing this way means you don''t have to tell the singleton your done with it, it''ll be killed (and it''s dtor called) when the program ends. Just make sure you don''t use any other singletons in the dtor cos they might be already gone.

Share this post


Link to post
Share on other sites