Reference
A reference is an alias for an existing variable.
It does not create a new object. It doesn’t take any memory
It is just another name for the same memory.
int x = 10;
int& ref = x;
1. Reference must be initialized
int& r; // error
// Reference cannot be NULL
// Unlike pointers, references must refer to something valid...
// This alone makes them safer than pointers.
int x = 10;
int& r = x; // valid
2. Reference must match the type
int x = 10;
double& r = x; // error
3. Reference cannot be changed to refer to another variable
int x = 10, y = 20;
int& r = x;
r = y; // assigns y's value to x, does NOT rebind reference
After this:
x = 20
y = 20
r still refers to x.
Last modified: 08 February 2026