CMap g_Map would be fine if either...
g_Map is outside the scope of a function.
or g_Map is made static.
example
// map.hclass CMap { ...... };extern CMap g_Map;
// map.cppCMap g_Map;
or
// map.hclass CMap { ...... };extern CMap* GetMap();
// map.cppCMap* GetMap(){ static CMap g_Map; return &g_Map;}
in both case, g_Map will be on the heap, not on the stack.
However, as so rightly said above, it is better to have it as a pointer, to control the time it gets created. Basically, the global variables are created at program startup, but in no specific order, which could cause problems.
If you use the example2, the static variable will be allocated at startup (I think), but constructed when you first call GetMap(). So you ''kind'' of are able to control the time g_Map gets created.
But to be on the safe side, have something like
// map.hclass CMap { ...... };extern CMap* InitialiseMap();extern CMap* GetMap();extern CMap* ShutdownMap();
// map.cppCMap* g_pMap = NULL;CMap* InitialiseMap(){ Assert(!g_pMap, "Map not shutdown."); g_pMap = new CMap(); return g_pMap;}extern CMap* GetMap(){ Assert(g_pMap, "Map not initialised."); return g_pMap;}extern CMap* ShutdownMap(){ Assert(g_pMap, "Map not initialised."); delete g_pMap; g_pMap = NULL;}
and you initialise.shutdown all your instances at your program init in one felt swoop, and in order.
even better, if using C++, have instanciation classes
class CMap{public: static void CreateInstance(); static CMap* GetInstance(); static void DestroyInstance(); Vector GetVertex(int i, int j) { ... } ..... ..... .....private: CMap(); ~CMap(); static CMap* g_pMap;};