Lesson 14 of 45
C++Lesson 14: References
8 min
Course progress31%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 14 of 45
C++8 min
Keep these ideas in mind as you work through the lesson.
References provide an alias for an existing object. Unlike pointers, a reference must be bound at initialization and cannot be reseated. They are often used for function parameters to avoid copying while preserving a natural syntax.
int x = 10;
int& ref = x; // ref is another name for x
ref = 20; // x becomes 20
std::cout << \"x = \" << x << '\
';
// Const reference allows read‑only access without copying
const std::string& cs = std::string(\"immutable\");
std::cout << cs << '\
';
References are required for operator overloading (e.g., operator=) and for range‑based for loops when iterating by reference to avoid copies.
Tip: Use
const&when you need to pass large objects without modification.