mardi 21 avril 2015

C++ extern pointer

Vote count: 0

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?

asked 30 secs ago



C++ extern pointer

Aucun commentaire:

Enregistrer un commentaire