More effective C++: Use references when we already have something to refer to and never want to refer to anything else, or when implementing operators whose syntactic requires us to do so.
Both pointers and references refer to other objects indirectly, and we should distinguish their differences.
No null reference
A reference must always refer to some object, which implies that references must be initialized:
|
|
Having known this, if we have a variable whose purpose is to refer to another object, but it is possible that there might be no object to refer to, we should make the variable a pointer; on the other hand, if the design requires that the variable must always refer to a non-null object, we choose the reference.
As an implication, reference’s never-nullable property save us from the burden of testing the validity before using it:
|
|
No reassignment for reference
Another difference is that we can reassign pointers to refer to different objects, while reference always refers to the one it is initialized:
|
|
In general, use pointers when
- it’s possible that there’s nothing to refer to (set pointers to null), or
- it’s needed to refer to different things at different times (reassign where the pointer points)
use reference when we know there will always be an object to refer to and we will never refer to anything else other than the initial object.
Use reference for some operators
Another situation to prefer reference is when we’re implementing certain operators such as operator[]
, which needs to return something that can be used as the target of an assignment:
|
|
If operator[]
returned a pointer, last statement would be changed to this:
|
|
This makes v
look like a vector of pointers, which it’s not. Thus we may prefer using a reference as the return type of operator[]
(for an exception, see MECpp-item 30).