Ch9: References¶
TL;DR: Alias Semantic: A reference serves as an alias for an existing variable, sharing its exact memory address and acting as another name for the same variable.
โก Quick Reference¶
#include <iostream>
int main() {
int value = 45;
// type& ref = val - Declare mutable alias referencing existing variable.
int& ref_val = value;
// const type& ref = val - Read-only alias accepting lvalues and temporary rvalues.
const int& const_ref = 100; // Accept temporary rvalue (100) safely.
ref_val = 50; // Modifies "value" to 50 via reference alias.
return 0;
}
๐ง Core Concepts¶
- Alias Semantic: A reference serves as an alias for an existing variable, sharing its exact memory address and acting as another name for the same variable.
- Immutability of Binding: References must be initialized upon declaration and cannot be rebound to point to another variable, behaving like constant pointers.
- Const References: A const reference (
const T&) prevents value modification through the reference, although the underlying variable remains mutable if it was declared non-const.
โ ๏ธ Pitfalls (Quick Scan)¶
| Mistake | Fix |
|---|---|
| Declaring a reference without an initializer | Always initialize references with an lvalue at the point of declaration |
| Attempting to rebind a reference to a different variable using assignment | Remember that references are permanently bound to their initial variable |
| Printing a pointer directly instead of dereferencing | Use dereference operator *p to print the underlying value |
| Assuming a const reference makes the original variable immutable | Understand that const only restricts access through the reference itself |
Writing invalid syntax like const int& const ref{var} |
Do not add const qualifier after the reference symbol (&) |
| Using copy variables in range-based loops when modification is needed | Use reference loop variables auto& to modify container elements (Lecture 14.5) |
๐ Full Details¶
Cause โ Effect โ Fix with timestamp (click to expand)
* **Declaring a reference without an initializer** -> **Compile-time error stating reference variable requires an initializer** -> **Always initialize references with an lvalue at the point of declaration (09:22)** * **Attempting to rebind a reference to a different variable using assignment** -> **Reassigns value of original variable instead of changing target address** -> **Remember that references are permanently bound to their initial variable (06:30)** * **Printing a pointer directly instead of dereferencing** -> **Outputs the memory address instead of the pointed value** -> **Use dereference operator `*p` to print the underlying value (05:56)** * **Assuming a const reference makes the original variable immutable** -> **Value remains mutable through the original variable name** -> **Understand that const only restricts access through the reference itself (12:22)** * **Writing invalid syntax like `const int& const ref{var}`** -> **Compile-time error because reference itself is implicitly constant and cannot be modified** -> **Do not add const qualifier after the reference symbol (`&`) (12:14)** * **Using copy variables in range-based loops when modification is needed** -> **Modifications only affect temporary loop copies rather than array elements** -> **Use reference loop variables `auto&` to modify container elements (Lecture 14.5)**๐ Repo Files¶
14.2.DeclaringAndUsingReferences/main.cpp14.3.ComparingPointersAndReferences/main.cpp14.4.ReferencesAndConst/main.cpp14.5.ReferencesWithRangeBasedForLoops/main.cpp