So I'm writing a program which has a big class called oglapp, and a whole bunch of other classes using it. What I want to achieve is to have a super-global oglapp* across all my .cpps. My approach is to declare an oglapp* in the main.cpp, which has the entry point (which sets up the pointer and starts running the class), and I have an extern reference at the end of the header file of oglapp. In theory, if I use the oglapp class anywhere in my project I need to include the header for it, which includes the extern, and I'm good. This is sorta what I have:
//main.cpp
oglapp* app;
entrypoint(){app=new oglapp(); app->run();}
//oglapp.h
class oglapp { ... }; extern oglapp* app;
//classX.h
#include "oglapp.h"
classX { ... };
//classX.cpp
#include "classX.h"
void classX::somefunction(){app->dosomething();}
What I get is a big null reference error. I tried including the oglapp header from the .cpp of classX, also tried putting the extern oglapp* app; to each of the classXs individually, I tried everything I could think of. Also, weirdly enough, I don't get null reference errors in all the classXs. Some of them can use the app without a problem, some of them see it as a null pointer. I've yet to discover what determines if it's working or not.
What am I doing wrong? Also, if it's impossible to declare a single super-global pointer to a class this way, how should I go about doing it?
It'll work reliably for anything that is instantiated from within main
or anything that main
invokes. For objects at global scope, those are constructed before main
is run, so your pointer won't be set yet.
You can make everything get initialised from within main
or higher up the call stack, or you can make an oglapp
singleton.
Or, since it doesn't look like oglapp
takes any arguments to its constructor, you can extern
an actual instance and create one of those at global scope, rather than a pointer. Then you have to be wary of the static initialisation order fiasco (look it up!), but at least your other global objects will have a chance… if you're careful.