[Beginner] — References are the first thing C++ added that changes how you write every function signature. const is the first thing that changes how you think about them.
References
int x = 5;
int &ref = x; // ref IS x — an alias, not a copy, not a pointer
ref = 10; // x is now 10A reference is another name for an existing object. Compared to a pointer:
| Pointer | Reference | |
|---|---|---|
| Can be null | yes | no |
| Can be reseated | yes | no — bound at initialisation, forever |
| Must be initialised | no | yes |
| Syntax to use | *p, p->x | r, r.x |
| Arithmetic | yes | no |
int &ref; // ERROR — must be initialised
int &ref = x;
ref = y; // does NOT rebind — this ASSIGNS y's value into xThat last line is the one to internalise: after binding, a reference is the object, so every operation on it operates on the referent.
Use a reference when the thing must exist. Use a pointer when it might not. That’s the whole rule, and it’s why C++ signatures are mostly references while C’s are all pointers.
void modify(int &x) { x = 42; } // must have a value
void maybe(int *x) { if (x) *x = 42; } // may be nullUnder the hood a reference is usually a pointer the compiler dereferences for you. The difference is entirely in what the type system permits.
Passing arguments
The three ways, and when each is right:
void by_value(std::string s); // COPIES — expensive for big objects
void by_ref(std::string &s); // no copy, and CAN modify
void by_const_ref(const std::string &s); // no copy, CANNOT modify — the default
const T&is the default for anything larger than a pointer. No copy, no null, and the signature documents that you won’t modify it.
void by_value(int n); // for small types, by value is FASTERSmall trivially-copyable types — int, double, a pointer, a two-field struct — should go by value. A reference is an indirection; copying 4 bytes is cheaper than dereferencing. The usual threshold is “bigger than 2 pointers, or non-trivial to copy”.
There’s a fourth case once you know move semantics — taking by value to sink a parameter. → Copy, Move and the Rule of Five
The dangling reference
References don’t protect you from lifetime bugs:
const std::string &bad() {
std::string local = "hello";
return local; // DANGLING — local dies here
} // UB when the caller reads itconst int &r = vec[0];
vec.push_back(x); // may reallocate
std::cout << r; // DANGLING — the buffer movedThis is exactly the case Rust’s lifetimes exist to prevent, and it’s why “C++ is safer than C” is only partly true. The compiler will warn about the first (-Wreturn-local-addr) and cannot see the second.
Const references extend the lifetime of a temporary, which is a genuine and useful exception:
const std::string &r = make_string(); // the temporary lives as long as rThat’s how const T& parameters can bind to temporaries — which is why f("literal") works for void f(const std::string&).
const
const int x = 5; // can't modify x
const int *p; // pointer to const int — can't write *p
int *const p = &x; // const pointer — can't reassign p
const int *const p = &x; // neitherRead right to left, same as C. const int *const p is “p is a const pointer to a const int”.
C++ adds const member functions, which is where it becomes structural:
class Counter {
int n = 0;
public:
int get() const { return n; } // promises not to modify the object
void inc() { ++n; }
};
const Counter c;
c.get(); // fine
c.inc(); // ERROR — can't call a non-const method on a const objectThe const after the parameter list means “this method doesn’t modify *this”. It’s the mechanism that makes const T& parameters actually useful — through a const reference you can only call const methods.
const correctness
The discipline: mark everything const that can be. Parameters, member functions, local variables, member data where possible.
class Rect {
double w_, h_;
public:
double area() const { return w_ * h_; } // const
double width() const { return w_; } // const
void scale(double f) { w_ *= f; h_ *= f; } // not const, obviously
};
void print(const Rect &r) {
std::cout << r.area(); // only works because area() is const
}Forget one const on a getter and every const& parameter downstream breaks. This propagates, which is why const correctness is something you do from the start rather than retrofit.
mutable is the escape hatch for members that don’t affect logical state — a cache, a mutex:
class Widget {
mutable std::mutex mu_;
mutable std::optional<int> cached_;
public:
int value() const { // logically const...
std::lock_guard lock(mu_); // ...but must lock
if (!cached_) cached_ = compute();
return *cached_;
}
};Use it for exactly that. mutable on ordinary state is a lie in the signature.
constexpr vs const
const int a = f(); // runtime constant — can't change after init
constexpr int b = 42; // COMPILE-TIME constantconstexpr is a much stronger claim and can be used where a compile-time value is required (array sizes, template arguments). → constexpr
auto
auto x = 5; // int
auto s = std::string("hi"); // std::string
auto it = v.begin(); // std::vector<int>::iterator — saves real typing
const auto &r = get_thing(); // deduced, no copyauto deduces the type from the initialiser. It’s essential once templates and iterators arrive, where the real type is unwriteable.
The trap: auto strips references and const by default.
auto x = obj.get_ref(); // COPIES, even if get_ref returns T&
auto &x = obj.get_ref(); // binds by reference
const auto &x = obj.get_ref(); // the safe default for readingWriting auto x = ... in a range-for over big objects is a common accidental-copy bug:
for (auto item : big_vector) { } // copies every element
for (const auto &item : big_vector) { } // correct
for (auto &item : big_vector) { } // to modify
const auto&in range-for unless you’re modifying. This single habit removes a whole class of silent performance loss.
auto in function return position (C++14) is fine for short functions; it hurts readability on public APIs where the caller has to read the body to know the type.
nullptr
void f(int);
void f(char *);
f(NULL); // AMBIGUOUS — NULL is 0, which is an int
f(nullptr); // unambiguous — nullptr has type std::nullptr_tUse nullptr, never NULL or 0 for pointers. NULL in C++ is just 0, which is an integer, which breaks overload resolution and template deduction.
Related
- Classes and RAII — where
constmethods matter - Copy, Move and the Rule of Five — the fourth way to pass an argument
- C: Pointers — what references replace
- Rust: Borrowing — references with lifetimes attached
- C++ course map
Software engineering
Infrastructure & security
Computer science
Hardware & engineering