01 / 21
& const

Const References
in C++

Learn how const references let you pass data efficiently without making copies, keeping your programs fast and safe.

C++

What Is a Reference?

A reference is an alias: another name for an existing variable. Instead of creating a brand-new copy of the data, a reference simply points to the same object in memory.

int x = 42; int& ref = x; // ref is an alias for x ref = 99; // now x is also 99

What Does const Mean?

The keyword const tells the compiler: "this value must not be changed." Any attempt to modify a const variable results in a compile-time error.

const int limit = 100; limit = 200; // ERROR: cannot modify a const

Why Do Copies Matter?

When you pass an argument by value, C++ makes a full copy of the object. For small types like int, this is cheap. For large types like std::string or std::vector, copying can be extremely expensive.

Pass by Value Full copy created Caller Copy
Cost: \(O(n)\) memory + time
Pass by Const Reference No copy: just an alias Caller Same Object
Cost: \(O(1)\) constant
Larger objects = bigger savings with const ref

The Const Reference Rule

A const reference combines both ideas: it gives you read-only access to the original object without making a copy.

const std::string& name = original; // name refers to original // name cannot be modified // no copy was made

When to Use Const References

✓ Use const ref for
std::string, std::vector, structs, classes, and any type larger than a pointer (typically > 8 bytes).
★ Pass by value for
int, double, char, bool: small built-in types where copying is already as cheap as making a reference.

Three Ways to Pass Parameters

By Value By Reference By Const Ref
Syntax Type x Type& x const Type& x
Makes a copy? Yes No No
Can modify original? No (only the copy) Yes No
Performance Slow for large types Fast Fast
Safety Safe (isolated copy) Risky (can mutate) Safe (read-only)

How to Apply Const Refs

1
Identify the parameter type. Is it a built-in type (int, double, char, bool) or a larger type (string, vector, struct)?
2
Check if you need to modify it. If you only read the parameter, it is a candidate for const ref.
3
Apply the syntax. Change Type param to const Type& param in the function signature.
4
Compile and verify. If your function body accidentally writes to the parameter, the compiler will catch it.

Example 1: Printing a String

Before: Passing by value copies the entire string every time.

void greet(std::string name) { std::cout << "Hello, " << name << "\n"; }

After: Using const ref avoids the copy entirely.

void greet(const std::string& name) { std::cout << "Hello, " << name << "\n"; }

Example 2: Finding the Max in a Vector

int findMax(const std::vector<int>& nums) { if (nums.empty()) { throw std::runtime_error("Vector is empty"); } int best = nums[0]; for (int n : nums) { if (n > best) best = n; } return best; }

Example 3: A Struct Parameter

struct Student { std::string name; int grade; std::vector<double> scores; }; void printReport(const Student& s) { std::cout << s.name << ": Grade " << s.grade << "\n"; }

Visualizing Memory

Original Object "Hello, World!" addr: 0x7fff42 By Value (Copy) "Hello, World!" addr: 0x7fff98 (NEW) COPY By Const Ref (Alias) Points to 0x7fff42 Read-only access ALIAS const ref shares the same memory: fast and safe

What const Prevents

Because the reference is const, the compiler will reject any code that tries to modify the referenced object through that alias.

void process(const std::string& s) { s = "new value"; // ERROR: read-only s.clear(); // ERROR: modifying op s.push_back('!'); // ERROR: modifying op int len = s.size(); // OK: size() is const char c = s[0]; // OK: read-only access }

Const Ref Binds to Temporaries

A special bonus: a const reference can bind to temporary values (rvalues). A non-const reference cannot.

void show(const std::string& s); void edit(std::string& s); show("Hello"); // OK: const ref binds to temp edit("Hello"); // ERROR: non-const ref cannot

Common Mistakes

✕ Using const ref for int
Passing int or double by const ref adds unnecessary indirection. Just pass by value: the copy is as cheap as a pointer.
✕ Forgetting const
Using a plain reference& when you only read the data. This prevents binding to temporaries and falsely signals mutation intent.
✕ Returning a const ref to a local
Returning const& to a variable that goes out of scope creates a dangling reference: undefined behavior.

Summary Cheat Sheet

Scenario Recommended Style
Read a small built-in (int, double) Pass by value
Read a large object (string, vector) const Type&
Need to modify the original Type& (non-const ref)
Need a local copy to change Type (by value)
Returning from a function Return by value (usually)

Question 1 of 5

What is the primary benefit of passing a std::string parameter as const& instead of by value?

Question 2 of 5

Which function signature is best practice for a function that only reads a std::vector<int>?

Question 3 of 5

Why is it generally not necessary to pass an int by const reference?

Question 4 of 5

What happens if you try to call s.push_back('!') inside a function where s is declared as const std::string&?

Question 5 of 5

A const reference can bind to a temporary value (like a string literal). A non-const reference cannot. Why is this useful?