5.2.3 · D3C++ Programming

Worked examples — const correctness — const variables, const pointers, const member functions

3,657 words17 min readBack to topic

This page is the drill floor for the parent topic on const. The parent told you the rules; here we grind through every case class so you never meet a const situation you haven't already seen.

Before we start, one reminder we will lean on constantly:


The scenario matrix

Think of const as living in three worlds — plain variables, pointers, and member functions — and each world has "edge" cases. The table below enumerates every cell we must cover. Each worked example is tagged with the cell(s) it hits.

# World Case class What could go wrong / be subtle
C1 variable plain const reassign write after init
C2 variable uninitialized const no value ever possible
C3 pointer const int* p (pointer-to-const) write *p vs repoint p
C4 pointer int* const p (const-pointer) repoint p vs write *p
C5 pointer const int* const p (both) both writes blocked
C6 pointer pointing a const int* at a plain int is that legal?
C7 member fn calling non-const method on a const object which overload matches
C8 member fn const/non-const at() overload pair compiler picks by object-constness
C9 member fn mutable escape hatch write inside a const method
C10 limiting/UB const_cast on truly-const data the undefined-behavior cliff
C11 shallow-const const object with a pointer member pointee stays writable
C12 word problem designing a read-only API putting it all together

We'll now walk C1–C12 in 9 examples (some examples clear two cells at once). Every example makes you forecast first.


Example 1 — plain const variable · covers C1, C2


Example 2 — pointer-to-const · covers C3, C6

Read the type right to left: p is a pointer to a const int. So the int is protected through p, but p itself is a free bird. Look at the figure — the lock is on the box, not on the arrow.

Figure — const correctness — const variables, const pointers, const member functions

Example 3 — const pointer · covers C4

Now const is right of *, so it locks the pointer q itself. The arrow is glued down; the box is open. See the figure: the red lock is on the arrow's tail.

Figure — const correctness — const variables, const pointers, const member functions

For a deeper tour of the pointer machinery itself see Pointers and References in C++.


Example 4 — both const · covers C5


Example 5 — const object & method matching · covers C7


Example 6 — const / non-const overload pair · covers C8


Example 7 — mutable escape hatch · covers C9


Example 8 — shallow const & the const_cast cliff · covers C10, C11


Example 9 — word problem: design a read-only-safe temperature log · covers C12

Here is the full, compilable class and driver. Every name it introduces is used in the steps that follow — temps is the array of stored readings, n is how many are stored, cachedAvg is the last computed average, and dirty is a flag that is true when cachedAvg is stale.


Recall

Recall Cover the answers — one line each

const int* p — which line fails, *p = 9 or p = &b? ::: *p = 9 fails; p = &b is fine (pointee is locked, pointer roams). int* const p — which write is blocked? ::: p = &b (repointing); *p = 9 is allowed. Why must const int* const r be initialized at declaration? ::: The pointer part is const, so its value can only ever be set once — at the initializer. A const object — which methods may you call? ::: Only const member functions (their this is const T*). Why can mutable int hits change inside a const method? ::: It is exempt from bitwise const because it holds non-logical state (a counter/cache). Is writing through const_cast on an originally-const object defined? ::: No — it is Undefined Behavior. const Node n with member int* ptr — can you do *n.ptr = 9? ::: Yes; const is shallow, so the far pointee stays writable (only repointing ptr is blocked).