C++: References

Understand what references are, how they differ from pointers, and how to use them as function parameters and return types.

What Are References?

A reference is an alias, an alternative name for an existing variable. Once bound, every operation on the reference is performed directly on the original variable. There is no separate copy and no extra memory for the reference itself.

References

A reference must be initialized when it is declared and cannot be rebound to a different variable afterward.

  • Syntax: type& refName = existingVariable;
  • Must be initialized at declaration, int& r; is a compile error
  • Once bound, r and the original variable are the same object in memory
  • No separate storage is used, the reference is just another name for the same location

Reference Basics

C++

r and x refer to the same memory, changing r changes x.

Reference Variables

Reference variables behave exactly like the variable they are bound to. You can read from them, write to them, and pass them to functions without any special syntax, no * needed to access the value.

Using Reference Variables

Because a reference is permanently bound, it is safer than a pointer, it can never be null and never accidentally points somewhere else.

  • Read: cout << ref; no dereferencing needed
  • Write: ref = newValue; updates the original variable
  • A const reference: const int& r = x; lets you read but not write through r
  • References to const can bind to temporaries and literals: const int& r = 42;

Reference Variables

C++

A mutable reference and a const reference to the same variable.

Reference vs Pointer

References and pointers both allow indirect access to a variable, but they have key differences in safety and syntax. Understanding when to use each is one of the core skills in C++.

References vs Pointers

Prefer references when you always have a valid target; use pointers when you need null, reseating, or dynamic memory.

  • References: cannot be null, cannot be reseated, no syntax overhead
  • Pointers: can be null, can be reseated, require * to dereference and & to take address
  • References are safer for function parameters, caller guaranteed to pass a valid object
  • Pointers are necessary for dynamic memory (new/delete) and optional parameters (nullable)
FeatureReferencePointer
Must be initializedYes, at declarationNo, can be left uninitialized (dangerous)
Can be nullNoYes, can hold nullptr
Can be reseatedNo, bound for lifeYes, can point to a different variable
Dereferencing syntaxNone, use directlyExplicit * required
Address-ofNot needed (no concept)Stored explicitly with &
Use with new/deleteNoYes

References as Function Parameters

Passing by reference lets a function modify the caller's variable without using pointers. The call site looks identical to pass-by-value, but internally no copy is made and changes are reflected in the original.

Pass by Reference

Pass by reference is the idiomatic modern C++ way to let a function modify its arguments, cleaner than pointers and impossible to accidentally pass null.

  • Syntax: void func(int& x), caller passes the variable directly: func(myVar)
  • No copy made, efficient for large objects
  • Changes inside the function are visible after the call
  • Use const& when the function only needs to read a large object: void func(const string& s)

Reference Parameters: Swap

C++

The function modifies x and y directly, the caller sees the change.

const Reference Parameters

A const reference parameter avoids copying a large argument while also preventing the function from accidentally modifying it. This is the standard pattern for read-only function inputs in C++.

const T& Parameters

For any parameter larger than a pointer (strings, vectors, structs), const& is almost always the right choice when the function only reads the value.

  • Syntax: void func(const string& s)
  • No copy is made, as efficient as passing a pointer
  • The compiler enforces read-only access, any write attempt is a compile error
  • Can also bind to temporaries: func("hello") works because string temporary binds to const&

const Reference Parameter

C++

No copy, no modification, the most efficient way to pass a read-only string.

Returning References

A function can return a reference, which allows the caller to directly read or modify the referred variable. The referenced object must outlive the function call, never return a reference to a local variable.

Returning References

Returning a reference to a local variable is undefined behavior, the local is destroyed when the function returns, leaving a dangling reference.

  • Safe to return: reference to a global, a static variable, or a member of an object passed in
  • Unsafe: int& func() { int x = 5; return x; } (x is gone after the return)
  • Returning a reference enables the caller to assign through it: func() = newValue;
  • Used heavily in operator overloading to allow chained operations like cout << a << b

Returning a Reference

C++

The caller can read and write globalVal through the returned reference.

Knowledge Check

1. What is a reference in C++?

2. Can a reference be reassigned to refer to a different variable after initialization?

3. Which statement about references vs pointers is correct?

4. What does passing a reference parameter allow a function to do?

5. Why should you avoid returning a reference to a local variable?

6. What is the main benefit of a const reference parameter?