5.2.3 · D1C++ Programming

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

2,081 words9 min readBack to topic

This is the foundations page for the parent topic. Before we can read a monster like const int* const p, we must earn every symbol in it, from absolute zero. We go slowly. Nothing is assumed.


0. What is a "variable" really? (the box picture)

Before const can mean anything, we need to see what it modifies.

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

Look at the figure: the grey rectangle is the box, the word age floating above is the name, and the 10 inside is the value. Two separate things live here — the name and the value it currently holds. Keep that split in your head; const is a promise about one of these two things, and half of all confusion comes from not knowing which.


1. The symbol = — assignment, not equality

  • int x = 10; — make a box named x, and in the same breath store 10. This first store is called initialization (giving a box its very first value).
  • x = 20; — a later assignment, overwriting the box.

Test yourself now:

Initialization vs assignment — which happens in int y = 5;?
Initialization (the box's first value).
And in a following line y = 7;?
Assignment (overwriting an existing box).

2. The word const — a one-way valve

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

In the figure, an ordinary variable has a two-way arrow (read ↔ write). Adding const snaps a valve shut: the write arrow is crossed out in chalk-pink, only the read arrow (chalk-blue) survives. That is literally all const does — it deletes the write direction.


3. Types: int, double, char — the shape of the box

We need types now because const is written fused onto a type: const int, const char. The const and the int together describe one box: "a whole-number box that is read-only." Where you place const relative to the type is exactly what we decode in Section 6. See constexpr and compile-time evaluation for the stricter cousin of const that must be known while the program is still being compiled.


4. The & symbol — "address of"

To understand pointers (Section 5) we first need the idea of a memory address.

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

In the figure, boxes are drawn as houses on a street; the number under each house is its address. &age hands you the street number of the age house, not the person living there. Prerequisite reading: Pointers and References in C++.


5. The * symbol — two totally different jobs

This symbol confuses beginners because it wears two hats. We separate them here so Section 6 is clean.

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

Read the figure left to right: box p holds the number 1000 (an address). The chalk-blue arrow labelled *p walks from p to house 1000 and reads the 10 living there. So a pointer is a box holding a house-number, and * is the act of walking to that house.


6. Reading const with pointers — right-to-left

Now every symbol is earned, we can decode the parent's table. The rule:

Walk each case slowly, right-to-left:

Map this back to the two-box picture (Section 5): const before * freezes the far box (pointee); const after * freezes the near box (the pointer). This exact skill powers Function parameters — pass by value vs const reference, where a const& parameter says "I read your data, I won't wreck it."


7. class, this, and member functions

The last piece the parent needs: what const means after a function.


Prerequisite map

variable = named box

assignment with equals

type int double char

const = read-only valve

address-of ampersand

pointer star and dereference

const with pointers RTL

class object this

const member function

const correctness topic

Each foundation feeds the next: boxes and assignment let us define the const valve; addresses let us define pointers; pointers plus the valve give the right-to-left reading; classes plus the valve give const methods. Together they become the full topic.


Equipment checklist

Cover the right side and test yourself. If any line is shaky, re-read its section.

A variable is...
a named box in memory holding a value.
The difference between initialization and assignment
initialization is the box's first value at birth; assignment is any later write.
What const deletes
the write direction — read stays, write is forbidden.
Why a const variable must be initialized immediately
the birth-write is the only write ever allowed.
What &x gives you
the memory address (house-number) of box x, not its value.
The two jobs of *
in a declaration it marks a pointer; in an expression it dereferences (follows the address).
Why pointers make const ambiguous
there are two boxes — the pointer and the pointee — so const could freeze either.
const int* p freezes...
the pointee (can't do *p = ...), but the pointer can be repointed.
int* const p freezes...
the pointer itself (can't repoint), but the pointee is writable.
What this is inside a method
a hidden pointer holding the address of the object the method was called on.
What const after a method does to this
turns it into const ClassName*, making all members read-only inside.
Which methods a const object can call
only the const member functions.