Pass by reference Vs Pointer

I’m new to this, and it might have been answered before, but I still can’t grasp the distinction between passing by pointer and passing by reference. In Unreal Engine’s source code, they often use pointers, but occasionally use references. Both can be interchanged and still function and it’s the same in terms of performance, as far as I understand. Is there a significant advantage in using pointers over references, or vice versa, that I should understand?

The only reason to use a reference is aesthetics/cleanliness.

References are effectively const pointers. Meaning you can change what the contents are, but not what contents you’re pointing to. There’s nothing that references can do that pointers can’t, but there are things that references can’t do.

References are cleaner though- you don’t have to use the * to de-reference. A reference is basically treated as you were directly using the passed variable.
No *Pointer = NewContents; Pointer->Something(); now you just have Pointer = NewContents; Pointer.Something(); If you can easily get away with using a reference, you should use a reference, but there’s no harm in using pointers.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.