Module lessons (2/2)
Passage by value and reference
In-memory copies vs aliases with the (&) reference operator. Change external parameters and use const T&.
What you'll learn in this lesson
- Distinguish in memory passing by value (isolated copy) from passing by reference (
T&, direct alias). - Modify external variables safely using
T¶meters without pointer syntax. - Avoid expensive copies of
std::stringandstd::vectorby always applyingconst T&as read-only.
Overview
In C++ you have complete control over how much work the CPU does when passing each argument: you can pass a small copy by value or bind a name directly to the original cell with a reference (&).
By default C++ passes each argument by value, creating a copy in the stack frame of the called function. Changing the local parameter leaves the caller variable intact:
Sections covered in this lesson
- 11. Pass by Value: Isolated Copy
- 22. Pass by Reference (
T&): Direct Alias - 33. Passage for
const T&: Zero-Copy Reads on Large Objects - 44. Practical Decision Table
- 5Practical exercises
- 6Check for understanding
Unlock "Passage by value and reference" and the full Course C++ curriculum
In-memory copies vs aliases with the (&) reference operator. Change external parameters and use const T&.
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in Course C++ plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.