01 / 21
& x

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

Memory Address 0x7ff... 42 x r Both names point to the same location Changing r changes x (and vice versa)

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

lvalue (has address) int x = 10; arr[0] *ptr int& ref = x; ✓ const int& cr = x; ✓ rvalue (temporary) 42 x + y func() int& ref = 42; ✕ const int& cr = 42; ✓

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 }
Before swap() x 3 y 7 After swap() x 7 y 3 Parameters a & b are aliases for x & y Changes inside swap() affect the caller's variables

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

func() stack frame int local = 5; destroyed on return ✕ return ref Caller scope int& r = func(); r is dangling! ✓ Safe: return by value instead int func() { int local = 5; return local; }

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;

Question 2 of 5

Which of the following will not compile?

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;

Question 4 of 5

What is wrong with this function?
int& bad() { int local = 5; return local; }

Question 5 of 5

Why does void process(const string& s) accept both lvalues and rvalues?