There are four main cases where you should use pass-by-reference over pass-by-value:
- If you are calling a function that needs to modify its arguments, use pass-by-reference or pass-by-pointer. Otherwise, you’ll get a copy of the argument.
- If you’re calling a function that needs to take a large object as a parameter, pass it by const reference to avoid making an unnecessary copy of that object and taking a large efficiency hit.
- If you’re writing a copy or move constructor which by definition must take a reference, use pass by reference.
- If you’re writing a function that wants to operate on a polymorphic class, use pass by reference or pass by pointer to avoid slicing.