A reference variable in C++ is an alias: a second name for an object that already exists. You declare it with &, as in int &ref = value;, and every read or write through ref acts on value itself. A reference must be initialised when it is declared and can never be rebound to another object.
References are the standard way to pass large objects to functions without copying them and to let a function modify its argument, without the pointer syntax that C requires.
The & after the type declares a reference. It is not the address-of operator in this position.
Taking the address of a reference gives the address of the referred object, which is why the comparison prints 1. The reference has no separate identity you can observe.
int &r; does not compile (except for function parameters and class members, which are bound by the caller or constructor).ref = other; copies the value of other into the original object. It does not make ref refer to other.int &r = 5; is an error, but const int &r = 5; is allowed because a const reference can bind to a temporary and extends its lifetime.A reference parameter lets a function change the caller's variable, or read a large object without copying it.
Use const T & for read-only access to anything bigger than a few machine words. Small types such as int, double and pointers are cheaper to pass by value.
Output parameters work, but modern C++ usually returns a struct or a tuple and unpacks it with structured bindings (C++17).
| Feature | Reference | Pointer |
|---|---|---|
| Can be null | No | Yes (nullptr) |
| Must be initialised | Yes | No |
| Can be reassigned | No | Yes |
| Access syntax | Use the name | *p, p->member |
| Pointer arithmetic | No | Yes |
| Own storage you can observe | No | Yes (&p is a different address) |
Choose a reference when the object must exist and you do not need to rebind. Choose a pointer, or std::optional, when "no object" is a valid state, or when you need to point at different objects over time.
C++11 added rvalue references, written T &&. They bind to temporaries and enable move semantics, which transfer resources instead of copying them. Inside a template, T && can also be a forwarding reference, used with std::forward. Start with lvalue references before learning these.
std::vector reallocation invalidates it.ref = other to rebind. Use a pointer or std::reference_wrapper if you need that.const T & members to temporaries in a constructor, which leaves the member dangling after the statement ends.Build with -Wall -Wextra and run AddressSanitizer to catch dangling references early.
A reference is an alias that must be initialised and cannot be null or reseated. A pointer is a separate object holding an address that can be null, reassigned and incremented.
The language does not require it to. A compiler often implements a reference as an address, but you cannot observe that, and for local aliases it frequently emits no storage at all.
const T & for parameters?It avoids copying large objects while guaranteeing the function does not modify them. It also accepts temporaries, so calls such as print("text") work.
No. References are not objects, so you cannot form arrays of them or pointers to them. Use std::reference_wrapper<T> in a container, or store pointers.
A C++ reference is a permanent alias for an existing object. It must be initialised, cannot be null or rebound, and lets functions modify or read arguments without copying. Prefer const T & for large read-only parameters, pointers or std::optional for optional values, and avoid returning references to locals.