Intermediate⏱️ 7 min📘 Topic 15 of 32

🔗 References in C++ — Aliases vs Pointers Explained

Understand C++ references — aliases for existing variables, how they differ from pointers, and when to use them in function parameters. With examples and interview Q&A.

A reference is an alternative name (alias) for an existing variable. Once bound, it always refers to the same object — it can't be reseated, and it can't be null.

📜 Syntax

int x = 5;
int& ref = x;     // ref is an alias for x
ref = 10;
std::cout << x;    // 10

🆚 Reference vs pointer

ReferencePointer
Must be initializedYesNo
Can be nullNoYes
ReassignableNoYes

💡 The killer use case: function parameters

Pass-by-reference avoids copying large objects. Add const when you don't need to modify the input.

void greet(const std::string& name) {
  std::cout << "Hi " << name;
}

💻 Code Examples

Swap via references — cleaner than pointers

void swap(int& a, int& b) {
  int tmp = a;
  a = b;
  b = tmp;
}
int x = 1, y = 2;
swap(x, y);
std::cout << x << ' ' << y;
Output:
2 1

const reference for large objects

void printAll(const std::vector<int>& nums) {
  for (int n : nums) std::cout << n << ' ';
}
Output:
No copy, no modification — perfect for read-only access.

⚠️ Common Mistakes

  • Forgetting to initialize a reference at declaration — compile error.
  • Trying to 'reseat' a reference: `ref = otherVar` assigns the VALUE, not the reference.
  • Returning a reference to a local variable — same dangling problem as pointers.
  • Using non-const reference when const reference would suffice — leaks intent and may be slower for some optimizations.

🎯 Interview Questions

Real questions asked at top product and service-based companies.

Q1.What is a reference in C++?Beginner
An alias for an existing variable. Declared with `Type&`. Once bound, always refers to the same object. Cannot be null or reassigned.
Q2.Why use references over pointers?Beginner
Safer (no null, no need to dereference), cleaner syntax (`ref` instead of `*ptr`), and clear semantics (must always refer to something). Pointers are needed when null is valid or the binding might change.
Q3.Can a reference be reassigned to refer to a different variable?Intermediate
No. `ref = otherVar` copies otherVar's VALUE into whatever ref already refers to — it does not rebind. If you need rebindable indirection, use a pointer.
Q4.What is a const reference and why use it?Intermediate
`const Type&` — a read-only alias. Doesn't copy the object, and prevents modification. Standard for passing large objects (std::string, std::vector) as function parameters when you don't need to change them.
Q5.What is an rvalue reference (&&)?Advanced
Introduced in C++11. A reference to a temporary object — enables MOVE semantics. `std::string&&` binds to objects about to be discarded, allowing you to steal their resources instead of copying. Foundation of `std::move`.

🧠 Quick Summary

  • Reference = alias for an existing variable.
  • Must be initialized; cannot be null or reassigned.
  • Cleaner than pointers when null isn't needed.
  • `const T&` is the standard for read-only large-object parameters.
  • Pointer for optional/rebindable; reference for required/fixed.