5.2.2 · D3C++ Programming

Worked examples — References — lvalue references, difference from pointers

2,451 words11 min readBack to topic

The scenario matrix

Before solving anything, let us enumerate what "every case" even means for references. A reference has only a few axes it can vary along, so the matrix is small and we can genuinely cover all of it.

Cell Scenario class What varies Covered by
C1 Bind to a named variable (the happy path) write-through, address identity Example 1
C2 sizeof / identity — is there a second box? object identity Example 2
C3 Illegal binding — literal / temporary to int& rvalue vs lvalue Example 3
C4 const T& binds a temporary + lifetime extension const, rvalue Example 4
C5 Reference parameter (pass-by-reference) vs by-value copy vs alias Example 5
C6 Reference to an array element / chained alias aliasing a sub-object Example 6
C7 Degenerate / dangling — return reference to a local lifetime = 0 after return Example 7
C8 Reference member in a struct — must init in ctor list binding at construction Example 8
C9 Real-world word problem — a "score tally" applied aliasing Example 9
C10 Exam twist — reference reassignment looks like reseat = on an alias Example 10

The "signs and quadrants" of a trig topic become, for references, these binding categories: lvalue vs rvalue, const vs non-const, lives-long-enough vs dies-early. Every example below is tagged with its cell.


Example 1 — The happy path (Cell C1)


Example 2 — Is there a second box? (Cell C2)


Example 3 — Illegal binding: literal to int& (Cell C3)


Example 4 — const T& binds a temporary (Cell C4)


Example 5 — Reference parameter vs by-value (Cell C5)


Example 6 — Reference to an array element (Cell C6)


Example 7 — Dangling: reference to a local (Cell C7)


Example 8 — Reference member (Cell C8)


Example 9 — Word problem: shared score tally (Cell C9)


Example 10 — Exam twist: assignment vs reseat (Cell C10)


Active recall

Recall Cover the answers

Does sizeof(int&) on a double& t give 8 because of a hidden pointer? ::: No — it gives sizeof(double)=8 because sizeof sees the referent; the pointer myth just coincides here. Why does const int& r = 3+4; survive but int& r = 3+4; fail? ::: const ref binds and lifetime-extends the temporary; non-const ref needs a named lvalue. After int& alias=a; alias=b;, does alias now name b? ::: No — it copied b's value into a; the alias still names a (no reseat). Why must a reference member be set in the constructor's init list? ::: You can't reseat a reference, so it must be bound at construction, not assigned in the body. What is the value of a reference returned to a destroyed local? ::: Undefined — the object is dead; never rely on it.


Connections

  • Pointers in C++ — the reseatable, nullable cousin used to contrast every cell
  • Pass-by-value vs Pass-by-reference — Example 5's copy-vs-alias split
  • const correctness — Example 4's const T& rescue
  • Rvalue references and move semantics — where rvalue binding (Cell C3/C4) goes next
  • Dangling references and lifetime — Example 7's trap
  • sizeof and object identity — Example 2 and 6
In Example 1, what does &alias == &score evaluate to?
true — the reference has no separate address.
In Example 5, after addTen_ref(x) and addTen_val(y) with both starting at 5, what are x and y?
x=15 (reference edits caller), y=5 (value edits a copy).
In Example 10, after int& alias=a; alias=b; b=99; (a=1,b=2 start), what are a and b?
a=2 (value copied in), b=99; alias never reseated.
Why does sizeof(char&) give 1, not 8?
sizeof sees through the reference to the char referent; references are not separate objects.