References &
Binding
Master C++ references with the & operator, understand how references bind to objects, and learn the rules that govern reference lifetime and aliasing.
C++
What Is a Reference?
A reference is an alias: another name for an existing variable. Once a reference is bound to an object, every operation on the reference acts on the original object itself.
int x = 42;
int& r = x; // r is a reference to x
r = 99; // x is now 99
References Share Memory
Why References Matter
Efficiency
Pass large objects to functions without copying. A reference costs zero extra memory overhead.
Mutation
Let a function modify the caller's variable directly: swap, sort, or update in place.
Safety
References cannot be null and must always be initialized: fewer bugs than raw pointers.
The Binding Rules
1
Must be initialized. You cannot declare a reference without binding it:
int& r; is a compile error.2
Cannot be reseated. Once bound, a reference always refers to its original object. Assignment copies the value, not the binding.
3
Non-const lvalue references bind only to lvalues: named variables, array elements, dereferenced pointers.
4
const references can bind to rvalues too: temporaries, literals, and expressions.
lvalue vs rvalue Binding
Reference vs Pointer
| Reference (&) | Pointer (*) | |
|---|---|---|
| Initialization | Required | Optional (can be null) |
| Reseating | Not allowed | Allowed |
| Null | Never null | Can be nullptr |
| Syntax | Use like a normal variable | Needs * to dereference |
| Arithmetic | Not supported | Supported |
How to Use References
1
Declare with & next to the type. Write
int& r, not int &r (style preference, but be consistent).2
Initialize immediately. Bind the reference to an existing variable in the same statement.
3
Use it like the original. No special operators needed:
r = 5; sets the original to 5.4
Prefer const& for read-only access. If you don't need to modify the object, use
const int& to prevent accidental changes.5
Use in function parameters. Pass by reference to avoid copies:
void swap(int& a, int& b).Example: Basic Reference
#include <iostream>
using namespace std;
int main() {
int score = 85;
int& alias = score;
alias += 10; // modifies score
cout << score; // prints 95
return 0;
}
Example: Pass by Reference
void swap(int& a, int& b) {
int temp = a;
a = b;
b = temp;
}
int main() {
int x = 3, y = 7;
swap(x, y);
// x is now 7, y is now 3
}
Example: const Reference
void print(const string& msg) {
cout << msg << endl;
// msg = "new"; ERROR: cannot modify
}
int main() {
print("Hello, world!"); // rvalue OK
string s = "Hi";
print(s); // lvalue OK
}
Returning by Reference
int& getElement(int arr[], int i) {
return arr[i];
}
int main() {
int data[] = {10, 20, 30};
getElement(data, 1) = 99;
// data is now {10, 99, 30}
}
The Dangling Reference Problem
const vs non-const Rules
int a = 10;
const int b = 20;
int& r1 = a; // ✓ non-const ref to non-const
int& r2 = b; // ✕ cannot drop const
const int& r3 = a; // ✓ add const is fine
const int& r4 = b; // ✓ const ref to const
const int& r5 = 42; // ✓ binds to rvalue
Common Mistakes
✕ Uninitialized Reference
Writing
int& r; without binding it. The compiler will reject this: references must always be initialized.✕ Binding to rvalue
Writing
int& r = 5; tries to bind a non-const reference to a literal. Use const int& instead.✕ Dangling Reference
Returning a reference to a local variable. The local is destroyed on return, leaving the reference pointing at freed stack memory.
Reference Cheat Sheet
| Scenario | Syntax | Binds To |
|---|---|---|
| Non-const lvalue ref | int& r = x; |
lvalues only |
| const lvalue ref | const int& r = x; |
lvalues + rvalues |
| Pass by ref (modify) | void f(int& p) |
caller's variable |
| Pass by const ref | void f(const int& p) |
any expression |
| Return by ref | int& f() |
non-local object |
Question 1 of 5
What does the following code print?int x = 10; int& r = x; r = 20; cout << x;
Since r is a reference to x, assigning 20 to r changes x. The output is 20.
Question 2 of 5
Which of the following will not compile?
A non-const lvalue reference cannot bind to an rvalue (the literal 42). You need const int& to bind to temporaries.
Question 3 of 5
After this code runs, what is the value of a?int a = 1, b = 2; int& r = a; r = b; b = 99;
r = b copies the value of b (which is 2) into a, because r is an alias for a. References cannot be reseated. Later changing b to 99 does not affect a. So a = 2.
Question 4 of 5
What is wrong with this function?int& bad() { int local = 5; return local; }
The local variable local is destroyed when
bad() returns. The returned reference refers to memory that no longer exists: a dangling reference and undefined behavior.Question 5 of 5
Why does void process(const string& s) accept both lvalues and rvalues?
A const lvalue reference has a special rule: it can bind to rvalues (temporaries). The compiler extends the temporary's lifetime to match the reference, so the function can safely read it without copying.