A reference is an alias for an existing object: int& ref = x;. References must be initialized, cannot be rebound, and enable clean pass-by-reference semantics—unlike pointer reseating in C.
Pass by reference
void increment(int& n) { ++n; }
int x = 5;
increment(x); // x becomes 6
const references
const int& gives read-only access without copying large objects—common in function parameters and range-for loops.
Important interview questions and answers
- Q: Reference vs pointer?
A: References must bind to a valid object, cannot be null (unless referring to nullable wrapper patterns), and use.syntax; pointers can be null and reseated. - Q: When use const T&?
A: For input parameters to avoid copies while preventing modification.
Self-check
- Can a reference be left uninitialized?
- What happens to the caller's variable with pass-by-reference?
Tip: References cannot be null and must bind at initialization—safer than pointers for function parameters that always exist.
Interview prep
- Reference vs pointer?
References must bind to a valid object, cannot be null, and cannot be reseated; pointers are more flexible but error-prone.
- When use const T&?
For read-only input parameters—avoids copies while preventing modification.