5.2.2 · D1C++ Programming

Foundations — References — lvalue references, difference from pointers

1,887 words9 min readBack to topic

Before you can use references confidently, you have to be able to read every squiggle the parent note throws at you. This page walks the whole alphabet of that topic, from the very idea of a "box in memory" up to const T&. Nothing is assumed. If you already know a rung, climb past it.


0. The picture underneath everything: a variable is a labelled box

Before any & or *, you need the mental model that all of this sits on.

Figure — References — lvalue references, difference from pointers

Look at the figure. Two separate things exist:

  • the box (a physical location — it has an address, a house-number),
  • the label x (the name you use to talk about it).

The whole references topic is about labels vs boxes. Keep that split in your head.

Why the topic needs this
A reference adds a second label to an existing box; you cannot understand "second label" until you have "box" and "label" as separate ideas.

1. The symbol = — and its two totally different jobs

The parent note warns you: = does not always mean "copy". It has two jobs.

Why the topic needs this
Half the "gotchas" about references come from mistaking binding for copying. Two jobs, one symbol.

2. The symbol & used as address-of — the house-number operator

& is the most overloaded symbol here. First meaning: an operator you put in front of a name.

Figure — References — lvalue references, difference from pointers

In the figure, each box sits at a numbered slot. &x reads off that slot number. This is the tool that lets us ask the parent note's key question: "do two names point at the same box?" — we just compare their addresses with ==.

Why the topic needs this
&r == &x is the parent note's whole argument that a reference has no box of its own.

3. The symbol & used in a type — declaring a reference

Now the confusing part: the same character & means something different depending on where it sits.

Why the topic needs this
The parent writes both int& r and &r on nearly every line; you must instantly know which & you're looking at.

4. The word lvalue — a thing with a fixed address

The parent calls references "lvalue references". You cannot skip this word.

Figure — References — lvalue references, difference from pointers

The figure sorts expressions into two bins: has an address (lvalue — can be aliased) vs no address (a temporary — cannot be aliased by a plain int&).

Why the topic needs this
This is exactly why int& r = 5; is illegal — 5 is not an lvalue, so there is no box to alias.

5. The symbol * — the pointer's dereference, for contrast

References are best understood against pointers, so you need the pointer alphabet too.

Figure — References — lvalue references, difference from pointers

Compare the two figures in your head:

  • Reference: one box, two labels — no arrow, no second box.
  • Pointer: two boxes — the pointer's own box holds a house-number that arrows to the target box. You must follow the arrow (*p) to reach the value.
Why the topic needs this
The entire comparison table in the parent ("separate object? nullable? reseatable?") is just this figure spelled out.

6. const — the promise "I won't write here"

The parent's const T& needs one more word.

Why the topic needs this
This is why const int& r = 5; compiles while int& r = 5; does not — the read-only promise makes aliasing a temporary safe. See const correctness.

7. sizeof — measuring a box, and why references confuse it

Why the topic needs this
It's the parent's hard evidence that a reference isn't its own object.

Prerequisite map

variable = named box in memory

address-of and r

assignment vs binding

lvalue - thing with an address

lvalue reference T and r

pointer T star and star p

const - read only promise

sizeof - measure the box

Read it top-down: everything grows out of "a variable is a box". Address-of and the lvalue idea feed the reference; the reference contrasts with the pointer; const, sizeof, and lifetime hang off both.


Where to go next

  • Parent: References — lvalue references, difference from pointers
  • Pointers in C++ — the reseatable, nullable cousin you just met in §5
  • Pass-by-value vs Pass-by-reference — why aliasing a caller's box matters
  • const correctness — the promise from §6
  • Rvalue references and move semantics — what temporaries (§4) unlock
  • Dangling references and lifetime — what happens when a box dies but a label survives
  • sizeof and object identity — §7 in full

Equipment checklist

Recall Am I ready? (cover the answers)

In int x = 10;, what two separate things exist? ::: A box (memory slot with an address) and a label x on it. What does &x evaluate to? ::: The address — the house-number of x's box. In int& r = x;, does = copy or bind? ::: It binds — glues the alias r to x's box; no copy. How do you tell address-of & from reference-declaration &? ::: Position: before a value = address-of; after a type = reference. What is an lvalue? ::: An expression naming a persistent box with an address (can sit left of =). Why can't int& r = 5; bind? ::: 5 is not an lvalue — it has no address/box to alias. What is *p? ::: Dereference — go to the address the pointer holds and open that box. What can a pointer be that a reference cannot? ::: nullptr (point at nothing) and reseatable to another box. What does const int& r promise? ::: Read-only access through r; never write, so it may even alias a temporary. What does sizeof(r) give for a reference? ::: sizeof of the aliased object — a reference has no box of its own.