Learn C++
#include "arrayspointers/references.hpp"

References in C++

//concept

A reference (int& r = x;) is an alias — a second name for existing storage, not a copy and not a nullable pointer. It must bind at declaration and can never rebind, which removes whole categories of pointer bugs (no null, no arithmetic, no reseating) while keeping zero-cost access.

References dominate function parameters (const& for inputs, & for outputs) and range-based for loops. Rvalue references (&&) extend the idea to temporaries, powering move semantics — same syntax family, different job: stealing resources instead of copying them.

//how it works
1

Alias, not object

int& r = x; makes r another name for x — no new storage, no null state, no separate address to manage.

2

Bind once, forever

A reference binds at initialization and cannot be reseated — r = y later assigns y's value into x, never rebinds r.

3

Parameters are the home turf

const std::string& in (read-only view) and int& out (writable slot) cover nearly every function signature worth writing.

4

&& is a different beast

Rvalue references bind temporaries and enable moves — same & family look, but about ownership transfer, not aliasing.

//example
#include <iostream>
 
void addTax(double& price) { price *= 1.1; }
 
int main() {
double price = 100.0;
addTax(price);
int& alias = (int&)price;
(void)alias;
std::cout << price << std::endl;
return 0;
}
Output
110
1addTax takes price by mutable reference, so *= 1.1 rewrites the caller's variable directly.
2No copy was made on the call — the function worked on the original storage the whole time.
3110 prints, proving the caller's price changed through the reference parameter.
//use cases
In-place modification through & parameters without pointer syntax
Zero-copy read access with const& for strings, vectors, and objects
Range-based for writes via for (auto& x : v)
Operator overloads (operator=, operator[]) that must return assignable lvalues

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C++ yourself — building beats watching.