PDA

View Full Version : Accessing the global application instance



Iori Branford
01-05-2007, 04:59 AM
Is there a proper way to do this, you know, from outside my Application.cpp and Application.h? Should it even be done?

I have an input event handler class which should work by translating the events into calls to application functions/signals (which either issue a command to game logic or adjust some application parameter). I thought it would be as simple as marking the application instance as static and then #including Application.h.

But it seems that the instance which the input handler sees is NOT the global one I want -- from what the debugger and my own investigations tell me, it is a new instance whose members have yet to be initialized. I've tried redeclaring the global instance as extern, and it hasn't helped. What do I do?

Seth
01-05-2007, 06:53 AM
Well, I'm not totally sure if this is what you mean, but I do access my "main instance" everywhere, should be fine.

In my main.cpp:


App MyApp; //declare the main app global
App * GetApp(){return &MyApp;}

In my main.h:



class App : public CL_ClanApplication
{
public:
App();
virtual ~App();
..blah blah a bunch of junk specific to the app...
};

extern App MyApp;
App * GetApp();

And then I use GetApp()->SomeFunction() everytime I need something from outside the MyApp class.

Iori Branford
01-05-2007, 07:01 AM
It turns out that, yes, my improper use of extern was the problem. Declare the global instance in Application.cpp, and again as extern in Application.h, so all a class need do is #include Application.h.