Archived

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

Kaezin

What do YOU use pointers for?

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
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
Thanks for the replies everyone, I''m just gonna stick with non-functions then. My g_Map was global in the earlier example.

If I wanted to initialize the map at a certain time why couldn''t I just call something like g_Map.Init() when I want it all to be set up properly?

Share this post


Link to post
Share on other sites
You are right, normally you don''t need pointers, or a variable is a pointer (like an object or interface) but can be used normally without really caring it''s a pointer.

You can make data structures that hold more than anything trivial into a class and have all functions that operate on that data methods.

You mention a pointer to your map class, but this is already implied if you have an object (a variable of type class xxx). So in your game class you just declare a variable of type cmap and create it (with new in C++, cmap.create in delphi) and then use it (and destroy it afterwards). So it''s a pointer but you never have to bother with *p or ^p.

The only times you need pointers is when you call functions that output a HRESULT and put output into memory pointed to by parameters.

Share this post


Link to post
Share on other sites
"I use pointers where my experience tells me to do so. To use that tactic you need to practice C++ instead of letting people tell you how to theoreticly use C++."

You, sir, are a moron. After all, these are forums. Forums are for questions. Am I asking them to write my map class for me? No. I''m asking so that I can know.

Go back into your cave, troll.

Share this post


Link to post
Share on other sites
quote:
Original post by Kaezin
You, sir, are a moron. After all, these are forums. Forums are for questions. Am I asking them to write my map class for me? No. I''m asking so that I can know.

Go back into your cave, troll.

Um, not the best way to win fans over. You asked a question, he gave an answer, that is what forums are for right? I am sure he didn''t really mean that as an insult, the "For Beginners" forum is for people who aren''t that experienced in programming, so that is probably just an assumption on his part that you need more practice. Although I disagree slightly with his opinion, I would prefer to actually have some theoretical background before I attacked the practical applications, simply because it would lead to better programming practices. Pointers are one of the most difficult concepts to grasp in the C++ language, and it does take time and practice before someone can really get a hold of them for their own purposes. So look at what each of types do, and then use reason to choose the best one based on what your program requires.

Share this post


Link to post
Share on other sites
quote:
Original post by bastard2k5
Pointers are one of the most difficult concepts to grasp in the C++ language
Of the concepts that are particular to C++, surely pointers is one of the simplest?

Share this post


Link to post
Share on other sites
Not really for someone that goes from another language to C++, particularly languages with garbage collection, like java for instance. Being able to use a pointer type is realitively simple, yes, but it probably creates some of the more common bugs, and usually when I have been programming in Java for a while(my CS courses are in Java, and only Java) I have to adjust for a while just to get used to handling my own memory. I don''t consider any other concept of the C++ programming language any more difficult than pointers.

Share this post


Link to post
Share on other sites
quote:
Original post by bastard2k5
I don''t consider any other concept of the C++ programming language any more difficult than pointers.
Are we talking about the same C++? I am not talking about "C with classes".

Really, pointers as a concept is trivial to anyone knowing what computer memory is (not everyone do). I am not saying that using them is always trivial.

Share this post


Link to post
Share on other sites
For someone who does NOT understand how memory works, pointers can be a problem. And so can everything else, because it''s a requirement for thorough understanding.

Shoot, I didn''t understand how they worked for a loooong time, because I didn''t understand how memory worked. Of course, I''m at a uni now and can actually BUILD a RAM unit

Once you understand concepts in computer science, you''ll see that it''s not the language that''s hard. What is hard is how you use it.


daveandrews.org - a Christian Programmer''s Weblog

Share this post


Link to post
Share on other sites
quote:
Original post by bastard2k5
quote:
Original post by Kaezin
You, sir, are a moron. After all, these are forums. Forums are for questions. Am I asking them to write my map class for me? No. I''m asking so that I can know.

Go back into your cave, troll.

Um, not the best way to win fans over. You asked a question, he gave an answer, that is what forums are for right? I am sure he didn''t really mean that as an insult, the "For Beginners" forum is for people who aren''t that experienced in programming, so that is probably just an assumption on his part that you need more practice. Although I disagree slightly with his opinion, I would prefer to actually have some theoretical background before I attacked the practical applications, simply because it would lead to better programming practices. Pointers are one of the most difficult concepts to grasp in the C++ language, and it does take time and practice before someone can really get a hold of them for their own purposes. So look at what each of types do, and then use reason to choose the best one based on what your program requires.


He wasn''t trying to help me. It was a flame.

If he wanted to help me, he would have said "I use pointers when my experience tells me to. Just work with them some more and you''ll get the hang of it."

Share this post


Link to post
Share on other sites
quote:
Original post by Kaezin
He wasn't trying to help me. It was a flame.
I cannot see how it was so. He adviced that you don't listen blindly what people in a forum say, but learn from practice. In my opinion, that is a good advice.
quote:
If he wanted to help me, he would have said "I use pointers when my experience tells me to. Just work with them some more and you'll get the hang of it."
Sorry to have to point you to it, but do read http://www.catb.org/~esr/faqs/smart-questions.html, especially the section Dealing with rudeness and On Not Reacting Like A Loser.

[edited by - CWizard on November 19, 2003 11:05:14 PM]

Share this post


Link to post
Share on other sites