Pointer vs. Reference

3

Original author of the question

link

  

Jack Reza link

What would be better practice when passing the original variable to the function to work with it by:

unsigned long x = 4;

void func1(unsigned long& val) {
     val = 5;            
}
func1(x);

or by:

unsigned long x = 4;

void func2(unsigned long* val) {
     *val = 5;
}
func2(&x);

Is there any reason to choose one over the other?

    
asked by Angel Angel 05.12.2015 в 08:20
source

3 answers

5

Pointers and references are conceptually the same, although there are subtle differences between them. The most remarkable of them (and probably, the least important, although more comfortable and readable), is that the references do not require the arrow syntax -> needed by the pointers to access members (in the case that an object ( class ) or registration ( struct ) is targeted), or access operator * , but use the same name to access the value, and operator . , which would be used with the same object, to access the members.

References were invented, in fact, to avoid having to use pointers when performing a step by reference (and for returns by reference), that is, when you want changes made within the function called to the object pointed impact on the calling function.

This code is relatively complex for what you really want to get:

unsigned long x = 4;

void func2(unsigned long* val) {
     *val = 5;
}
func2(&x);

While the version with references is much more readable:

unsigned long x = 4;

void func1(unsigned long& val) {
     val = 5;            
}
func1(x);

So, as for the specific question, I certainly think that the use of references is the most appropriate. The underlying question is: can references be always used instead of pointers? The answer is no, because the references carry several inherent limitations to its operation:

  • References can not be initialized to NULL or nullptr. Moreover, they can not even be created without being initialized. And once initialized, you can not change the object or value they point to.

  • Pointer arithmetic can not be used with a reference. For example, you can not traverse an object vector with a reference, because you can not change the object it points to once it has been created.

Thus, the following code can not be written with a reference:

Persona * p = personas;

for(; ( p - personas ) < MaxPersonas; ++p) {
    cout << p->getNombre() << endl;
}

Hence the "golden rules" that appear in the other answer: if you need to point to more than one object, or you need to indicate that you do not point to anything (NULL value), or you need to use pointer arithmetic, then you can not Use references. When you can use them, however, do not hesitate to do so, because they are much more readable thanks to their syntax.

I hope I have helped you.

    
answered by 05.12.2015 / 11:12
source
2

Original author

  

Nils Pipenbrinck link

My golden rule is:

Use pointers if you want to do pointer arithmetic with them (for example, increasing the direction of the pointer to the step through an array), or if you ever have to pass a NULL-pointer.

Use references in another way.

    
answered by 23.05.2017 в 14:39
1

One important difference is that pointers can be compared with NULL and references can not.

Although these things are always subjective one of the most accepted style rules is that of Google ( link ) where you recommend passing arguments by reference only for values that never change (const). If the arguments can change, they recommend passing them as pointers to be able, among other things, to confirm that they are not NULL. More generally they say that references have pointer behavior but values syntax which makes them confusing.

    
answered by 05.12.2015 в 19:13