3 ms·
A reference is just a pointer in disguise, and so can still be null if created from a pointer. The rationale behind passing by ptr is at the call site you have
by nly 5y ago
A reference is just a pointer in disguise, and so can still be null if created from a pointer.
The rationale behind passing by ptr is at the call site you have to use the & operator, which makes it immediately obvious to the reader that it's possible the argument is being mutated (because it's not passed by value).
In general it's even better to avoid out parameters entirely, although there are cases where you want to reuse a containers memory where it's a useful technique.
- dataflow 5y ago> A reference is just a pointer in disguise, and so can still be null if created from a pointer. References cannot be null legally.
- bluGill 5y agoTrue, but I've seen people exploit undefined behavior to make a null reference. Please don't ask me to explain it, someone might think they understand it and try it.
- masklinn 5y ago> A reference is just a pointer in disguise, and so can still be null if created from a pointer. That’s an immediate UB right there.