Passing arguments by value, reference or pointer
Question
Compare passing function arguments by value, by reference and by pointer in C++.
Short answer
Value copies, reference aliases, pointer passes an address. By value the callee gets its own copy, so the caller is safe but large objects are expensive to copy. By reference there is no copy and the callee can modify the original (use const& for read-only); a reference cannot be null or reseated. A pointer can be null (useful for optional arguments) and needs explicit dereferencing, so null checks are the caller's burden.
Adapted from Joshi, Denson & Downes, Quant Job Interview Questions and Answers, Q7.26, p.228; question and answer rewritten by SelfMadeQuant.com.
See the hint and the full worked solution in the question bank (free account).
Open the worked solution