[c++]What is the difference beween & (reference) and * (pointer)?

Started by
5 comments, last by DevFred 14 years, 6 months ago
Hi mates! What is the difference beween & (reference) and * (pointer)? I mean, when passing arguments and returning a value. Are not they the same (but different notation, of course, -> and .)? Another question: When coding: std::string& GetWhatever() {return std::string("dsdasdas");} Why returned string is not lost? Thanks a lot for your valuable time.
I've seen things you people wouldn't believe. Attack ships on fire off the shoulder of Orion. I watched C-beams glitter in the dark near the Tannhauser gate. All those moments will be lost in time, like tears in rain. Time to die.
Advertisement
Quote:What is the difference beween & (reference) and * (pointer)?
The main difference is that references can't be NULL

Quote:std::string& GetWhatever() {return std::string("dsdasdas");}
Why returned string is not lost?
Because you return a reference to a temporary value. The return value is destroyed as soon as you leave the function (its scope), so you must return it as a copy like this:
std::string GetWhatever() {return std::string("dsdasdas");}


Similar situation here
void somefunction(){    int* pFoo;    {        int foo = 42;        pFoo = &foo;    }    // 'foo' does not exist here, it was destroyed when the scope ended    foo; // <-- compilation error, 'foo' does not exist    int bar = *pFoo; // <-- undefined value, because the variable that 'pFoo' points too no longer exist}


A & (reference) and a * (pointer) are entirely different things and learning the difference is crucial. I'm a little rusty on my programming at the moment but the reference IS a reference to a physical location in memory and a pointer IS a variable that points to that physical location!

As far as I can remember its very important that you understand that pointers do just what their name suggests - they point to a location in memory. That location is rarely a fixed object (which is an advantage of using a pointer) so one application for using pointers would be so that various different objects can all have access to a dynamic area of memory. A gaming example from the 4X genre would see a unit in the game as the dynamic area of memory - various other objects (such as a game controller/fight controller/unit update function etc) would point to this unit as they may each need variable that points to the unit, if the fight controller deals damage to the unit then the game controller would realise this as it has a variable which points to the unit, as a consequence the unit may 'die' from the damage and be removed from the game.

I rarely use references (which is probably a fault in my programming style) but I'm sure other posters will give you a more academic definition of the difference between refs and pointers.
Well, I would say that the second major difference is that you can have constant references to constant values.

void ref_func(const int& ref);void ptr_func(const int* ptr);ref_func(5); // OK! reference to the constant '5'ptr_func(&5); // Error! can't take the address of a constant valueint a = 5;ref_func(a); // OK! reference to the integer 'a'ptr_func(&a); // OK! pointer to the integer 'a'


@furiousuk
There are pros and cons of both and it's not always clear when to use which.

As for the academic difference, it's very complicated. I think the OP just wants an idea of where it's appropriate to use references and where pointers are better

I would say: use constant references as arguments, instead of pointers, when you want to make sure that the arguments are valid. In other words when you don't want to check for null-pointers.

Here's a contrived example
void func(const std::string& text){    cout << text;}void func(const std::string* text){   if(text == 0)       return;   cout << *text;}

OK, thanks a lot for your help. It is clearer ;)

BTW:

This is correct:
std::string GetWhatever() {return std::string("dsdasdas");}

This has no sense because returned data is destroyed as soon as we leave GetWhatever, Am I right?
std::string &GetWhatever() {return std::string("dsdasdas");}

Thanks.
I've seen things you people wouldn't believe. Attack ships on fire off the shoulder of Orion. I watched C-beams glitter in the dark near the Tannhauser gate. All those moments will be lost in time, like tears in rain. Time to die.
Quote:Original post by ricardo_ruiz_lopez
OK, thanks a lot for your help. It is clearer ;)

BTW:

This is correct:
std::string GetWhatever() {return std::string("dsdasdas");}

This has no sense because returned data is destroyed as soon as we leave GetWhatever, Am I right?
std::string &GetWhatever() {return std::string("dsdasdas");}

Thanks.


Correct.
Quote:Original post by furiousuk
a pointer IS a variable

A common mistake. Not every pointer is a variable, just as not every int as a variable.
int i = 5; // i is a variable, 5 is not, but both are intsint* p = &i // p is a variable, &i is not, but both are pointers

Another example for a pointer that is not a variable is p+0.

This topic is closed to new replies.

Advertisement