Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's C++ page.
A function argument can be passed by copy, reference or pointer.
void f(T t); //t is a copy of the value passed
void f(T &t); //t is the value passed
void f(T* t); //t is a pointer to the value passed passed
|
When a function argument is passed by reference,
the function can modify the original variable.
Prefer const reference arguments
to plain reference arguments [1].
Use const reference to express immutability in interfaces [2].
Prefer references to pointers as arguments, except where "no object" is a reasonable option [3].
Use pass-by-const-reference to pass large values that you don't need to modify, use pass-by-non-const-reference only if you have to [5].
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 7.8. Advice. page 199: '[9] Prefer const reference arguments to plain reference arguments'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 7.8. Advice. page 199: '[13] Use const pointers and const references to express immutability in interfaces'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 7.8. Advice. page 199: '[14] Prefer references to pointers as arguments, except where "no object" is a reasonable option'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 12.7. Advice. page 341: '[8] Use pass-by-const-reference to pass large values that you don't need to modify'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 12.7. Advice. page 341: '[12] Use pass-by-non-const-reference only if you have to'
Go back to Richel Bilderbeek's C++ page.
Go back to Richel Bilderbeek's homepage.
