original question author
https://stackoverflow.com/questions/114180/pointer-vs-reference
What would be best 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?
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 it is more comfortable and readable), is that references do not need the arrow syntax
->
that pointers need to access members (in the case that an object (class
) or record (struct
) is pointed to), or the access operator*
, but use the same name to access the value, and the operator.
, the one that would be used with the same object, to access the members.References were, in fact, invented to avoid having to use pointers when passing by reference (and for returns by reference), that is, when you want changes made within the function called to the pointed object to affect the calling function.
This code is relatively complex for what you really want to get:
While the version with references is much more readable:
So, regarding the specific question, I certainly think that the use of references is the most appropriate. The underlying question is: can references always be used instead of pointers? The answer is no, because references carry several limitations inherent to their operation:
References cannot be initialized to NULL or nullptr. Furthermore, they cannot even be created without being initialized. And once initialized, you can't change the object or value they point to.
You cannot use pointer arithmetic with a reference. For example, you can't iterate through a vector of objects with a reference, since you can't change the object it points to once it's created.
Thus, the following code cannot be written with a reference:
Hence the "rules of thumb" in the other answer: if you need to point to more than one object, or you need to indicate that nothing is pointed to (NULL value), or you need to use pointer arithmetic, then you can't use references. When you can use them, however, do not hesitate to do so, as they are much more readable thanks to their syntax.
I hope I've helped.
One important difference is that pointers can be compared to NULL and references cannot.
Although these things are always subjective, one of the most accepted style rules is that of Google ( https://google.github.io/styleguide/cppguide.html#Reference_Arguments ) in which it recommends passing arguments by reference only for values that never they change (const). If the arguments can change, they recommend passing them as pointers in order to, among other things, confirm that they are not NULL. More generally they say that references have pointer behavior but value syntax which makes them confusing.
original author
My rule of thumb is:
Use pointers if you want to do pointer arithmetic with them (for example, incrementing the pointer's address when passing through an array), or if you ever have to pass a NULL-pointer.
Use references in another way.