C++ Workshop - Pointers (Ch. 8)

Started by
87 comments, last by mihaimoldovan 12 years ago
I think I'm fairly comfortable with the idea of pointers now, although I wish they were called something else - '... and the pointer points to another pointer pointing at the pointed at value...' - if I can barely get my tongue round this just imagine what it's doing to my brain. But it's basically looking at the address on the letter instead of the Dear John letter inside. If she's not a constant woman she could change her mind and put another letter in the envelope.

But the 'this' pointer stumps me. I'm sure it will be covered in a later section as to why you would use it, and in preference to a regular pointer but it's hard to take things on faith when you're as old as me. For pointers in general it would be great to hear about real world uses for them. I imagine one example would be if you wanted a new footsoldier to be created in an RTS you would use a pointer to the free store to create a new object while the program was running. Are there any other examples for using pointers?
Advertisement
Moved to the week 6 topic.

[Edited by - RinusMaximus on July 19, 2006 4:39:55 AM]
Quote:Original post by RinusMaximus
I have a question which is not related to this Chapter, but to the ColorMenu exercise, but since this is the most active topic I'll post it here.

Since it is easy to spot new posts on an older topic, you should post your question in its correct chapter thread. It will prevent thread hijacking and long discussions about subjects that are not related to the current chapter, and it will also allow new reader to spot the answer to the same exact question if they need it. I'm posting my answer in the correct thread.

Regards,
Forewarning: I am not a tutor.


For pointers, it's sometimes useful when starting out to think of them like web links.

A link (pointer) by itself is pretty useless, but if you follow the link (dereference the pointer) you'll hopefully get to the website (data). Similarly, the usage and problems follow a similar pattern.

Would you copy the entire website to send it to a friend? No, you'd just send your friend a link. Why? Well, sending the entire website contents is a lot of work compared to a URL. Pointers too are far smaller than large data structures. More importantly though, sending your friend the link means they'll see the same site. If you send them a copy, they won't notice any changes or updates to the site itself. Anyone with a link can visit the site whenever they like to see updates. Anyone with a pointer can dereference it to see the data it points to, and all will see any changes made.

So what happens if the webserver is down, or you mis-type a link? Right, problems. Pointers have the same problems (though their problems are often more serious!). If the data the pointers point at gets deleted, it's akin to the web server being down. Links to the site still exist, but following them doesn't work. Pointers to 'garbage' (low values like 0x00000000, or high values such as 0xcdcdcdcd) are like mis-typed links and most often caused by not initializing them properly. Like links, they're fine as long as you don't follow (dereference) them. Most often they won't lead anywhere and break (crash your program). Sometimes they will point to something, but not what you wanted, leading to weird and hard to find bugs.
Quote:Original post by simesf
But the 'this' pointer stumps me.


Member functions are different from ordinary functions. They are "bound" to instances of the object, and implicitly take their own instance as a parameter. The 'this' pointer is simply a pointer that automatically points at "the current object".

class Foo {  int bar;  public:  void wibble() {    bar = 1; // the usual way    this->bar = 1; // making use of the 'this pointer'  }};int main() {  Foo x, y;  x.wibble(); // <-- 'this' points to x  y.wibble(); // <-- 'this' points to y}
What is the best way to return a pointer in a function?
Ex:
DataType *m_pPointer;

//This
DataType* Getpointer(void);

//Or this
DataType& Getpointer(void);

ok I know the the "&" returns the address of the pointer.
But returning with "*" symbol just creates a new pointer that will create a dangling pointer?
Just trying this learn this very challenging subject of game programming.
Quote:Original post by boolai
What is the best way to return a pointer in a function?
Ex:
DataType *m_pPointer;

//This
DataType* Getpointer(void);

//Or this
DataType& Getpointer(void);

ok I know the the "&" returns the address of the pointer.
But returning with "*" symbol just creates a new pointer that will create a dangling pointer?


[OPINION]0) Don't write (void) for function prototypes. Write (). This is technically an opinion, but it's an opinion that the inventors of C and of C++ happen to share. :)[/OPINION]

1) Nothing in the function prototype actually returns anything; it simply says what kind of thing you will return.

2) "DataType&" means you will return a reference to the thing. References and pointers are different things. You should read this.

3) "DataType*" means you will return a pointer to the thing. Whether that pointer is valid, or what it actually points to, depends on (the correctness of) the implementation code.

4) If the member is itself a pointer, then you have to be clear about whether you're returning a pointer or reference *to that pointer*, or *to the thing which is pointed at*. Note that by returning a reference to a pointed-at thing, you are effectively promising that the pointer is not NULL, because the reference must refer to a valid object (see the link).
Quote:Original post by boolai
What is the best way to return a pointer in a function?
Ex:
DataType *m_pPointer;

//This
DataType* Getpointer(void);

//Or this
DataType& Getpointer(void);

<see below>
But returning with "*" symbol just creates a new pointer that will create a dangling pointer?


The later don't return a pointer, it returns a reference. Only the first one returns a pointer. Both function, however, return their result by value, meaning that the first one returns a copy of a pointer and the second one returns a copy of a reference. Nothing is created at this point.

Lets do something simple:
class A{public:  // we simply return this  A* getThisPtr() { return this; }  // we have to dereference this to be able to return a reference  A& getThisRef() { return *this; }   // this method returns a <i>copy</i> of the object pointed by "this"  // again, we need to dereference this  A getThisCopy() { return *this; }};int main(){  A a;  // ptr points to a  // changing the content of what ptr reference will change a as well  A *ptr = a.getThisPtr();  // ref references a (changing the content of ref will change a as well)  A& ref = a.getThisRef();  // copy is a new object - a copy of a  // changing copy will not affect a  A copy = a.getThisCopy();}


Quote:Original post by boolai
ok I know the the "&" returns the address of the pointer.


Take care: the & operator is used in two different flavor: if it is used in conjunction with a type (as in int& someVar = someOtherVar;), it defines a reference (I guess you'll see them in a few days, so I'm not going to boggle your mind with this right now). If it is used in conjunction with a variable (as in int* ptr = &someVar), it is the address-of operator which returns a pointer to the variable.
In your case, "&" don't return anything - you are in the first case, and it is really a type modifier (as "*").

While I'm at it, take care of a well known problem: you can't return the address of something which lies on the stack (I believe that this issue is already addressed in the book).

Regards,
Quote:Original post by Emmanuel Deloget
Take care: the & operator is used in two different flavor: ...

Three. You forgot bitwise-AND.
Hey thanks for the info. Just another question.
Now let take the last function

DataType* GetPointer(DataType* p_pointer)
{
//create a temp pointer
DataType* TempPointer = new DataType;
//copy
m_pPointer = TempPointer;

//Do we need to delete TempPointer or will the function
//Do it for us?
delete TempPointer;

return m_pPpointer;
}//end of function

I know that c++ will delete the parameter first then any local variables and then do the return; Do we still need to delete the TempPointer from memory?
Just trying this learn this very challenging subject of game programming.

This topic is closed to new replies.

Advertisement