Jump to content
  • Advertisement

Archived

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

Kaezin

What do YOU use pointers for?

This topic is 5301 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m not exactly sure as to when I should use pointers, so I''ll get straight to the point: I have a class for handling my map so it will probably be holding a lot of variables. Would there be any point to using a global pointer to my general map class or should I just make it the old fashioned way? In other words, should I do
CMap *g_Map; 
or
CMap g_Map; 
Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
I use pointers for pretty much anything.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I use pointers to get a handle on memory

the second one

Share this post


Link to post
Share on other sites
When it comes to global objects, you should use a pointer to the object if you want to control exactly when that object is created.

If the object is statically allocated (as in, not a pointer), then you can''t control whether that thing is created first, or last, or in the middle. If the object is dynamically allocated (as in, using a pointer), then you obviously control when it gets created because you have to create it yourself.

Share this post


Link to post
Share on other sites
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.h

class CMap { ...... };

extern CMap g_Map;



// map.cpp

CMap g_Map;



or


// map.h

class CMap { ...... };

extern CMap* GetMap();



// map.cpp

CMap* 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.h

class CMap { ...... };

extern CMap* InitialiseMap();
extern CMap* GetMap();
extern CMap* ShutdownMap();



// map.cpp

CMap* 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;
};

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!