Choosing a parameter type

A quick guide to value, reference, and pointer parameters.

At a glance

Parameter Best suited for Can change the caller’s object? Can be null?
T value Small, inexpensive types or when a copy is needed No No
const T& value Larger read-only objects No No
T& value Required mutable objects Yes No
T* value Optional objects or pointer-based interfaces Yes Yes
void useValue(int value);
void readObject(const Widget& object);
void changeObject(Widget& object);
void useOptionalObject(Widget* object);

Choosing quickly

  • Use T when making a copy is cheap or useful.
  • Use const T& to avoid copying a required read-only object.
  • Use T& when the function must modify a required object.
  • Use T* when nullptr has a useful meaning.