5.2.3C++ Programming

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

2,174 words10 min readdifficulty · medium2 backlinks

1. const variables

WHY initialize immediately? Because there is no later moment when you're allowed to write to it. If you can't init now, you never can.

HOW the compiler treats it: A const int initialized with a compile-time constant can be folded into the binary (true constant). A const initialized at runtime is still a normal read-only memory slot.


2. const and pointers — read it RIGHT-TO-LEFT

This is where everyone trips. The trick: read the declaration right to left, and remember:

Declaration Read as Can change value? Can change pointer?
const int* p pointer to const int *p p
int* const p const pointer to int *p p
const int* const p const pointer to const int
Figure — const correctness — const variables, const pointers, const member functions

3. const member functions

WHAT does const do to this?

  • In a non-const method, this has type Circle* (or really Circle* const).
  • In a const method, this becomes const Circle* — so every data member is read-only inside it.

4. Common mistakes (steel-manned)


5. Active recall

Recall Quick self-test (cover the answers)
  • Why must a const variable be initialized at its declaration? → No later write is ever permitted.
  • int* const p — what can you change? → The pointed-to value, not the pointer.
  • What type does this have inside a const method? → const ClassName*.
  • Which methods can you call on a const object? → Only const member functions.
  • What keyword lets a data member change inside a const method? → mutable.
Recall Feynman: explain to a 12-year-old

Imagine a glass display case. const is the glass: you can look at the toy inside but not touch it. A "pointer to const" is a finger that can point at any case but can never reach in. A "const pointer" is a finger glued to ONE case but you're allowed to open that one. A "const member function" is a museum guide who promises only to show exhibits, never rearrange them — so even the locked (const) rooms let the guide in.


What is const correctness?
Using const to declare your intent not to modify data, letting the compiler enforce read-only-ness at compile time with zero runtime cost.
Why must a const variable be initialized immediately?
Because it can never be assigned to after declaration, so its only chance to get a value is at the declaration.
How do you read const int* const p?
Right-to-left: "p is a const pointer to a const int" — neither the pointer nor the pointed-to value can change.
In const int* p, what is protected?
The pointee (the int) — you can't do *p = ..., but you CAN repoint p.
In int* const p, what is protected?
The pointer itself — you can't repoint p, but you CAN do *p = ....
What does const after a member function's parameter list promise?
That the function won't modify the object's (non-mutable) data members; this becomes a const ClassName*.
Which member functions can be called on a const object?
Only those declared const.
What does mutable do?
Allows a specific data member to be modified even inside const member functions (for caches, mutexes, counters).
Is C++ const-ness deep or shallow?
Shallow (bitwise): a const object's pointer member becomes a const pointer, but the data it points to is not automatically const.
Why is const_cast-then-modify on an originally-const object dangerous?
It's undefined behavior; the const object may live in read-only memory.
What's the rule for which side const binds to?
It binds to the type on its left; if nothing is on its left, it binds to the type on its right.
Why prefer const int x over #define X?
const is typed, scoped, and visible to the debugger and compiler; #define is blind text substitution.

Connections

Concept Map

enforced at

enables

enables

requires

forbids

decoded by

const left of star

const right of star

used for

declares intent

promises no

const promise to compiler

const variable

must init at declaration

never reassigned

const with pointers

read right to left

pointer to const int

const pointer to int

const& parameters

const member function

Hinglish (regional understanding)

Intuition Hinglish mein samjho

const ka matlab hai ek promise jo aap compiler ko dete ho: "main is naam ke through data ko modify nahi karunga." Compiler ye promise compile-time pe enforce kar deta hai, bina kisi runtime cost ke. Iska sabse bada faayda — aapka intent type ka hissa ban jaata hai. Jaise void print(const string& s) bolta hai "main sirf padhunga, tumhara data kharab nahi karunga." Compiler free me bug-catcher ban jaata hai.

Pointers wala part sabko confuse karta hai. Trick simple hai: declaration ko right-to-left padho. const int* p = "p ek pointer hai jo const int ki taraf point karta hai" — yani *p = 5 nahi chalega (value locked), par p = &b chalega (pointer free). Ulta, int* const p = "p ek const pointer hai" — pointer locked, par *p = 5 allowed. Yaad rakho: const apne left wale type ko bind karta hai; agar left me kuch nahi to right ko.

Member functions me, function ke baad const lagao jab function sirf padhta hai, modify nahi karta — jaise double area() const. Iske andar this const ClassName* ban jaata hai, to koi bhi member change nahi kar sakte. Iska importance: agar aapke paas const object hai, to aap usme sirf const member functions call kar sakte ho. Isliye saari read-only methods ko shuru se hi const mark karo, warna baad me retrofit karte waqt poora cascade of edits ho jaayega.

Do common galtiyan: (1) const ko shallow samjho — const object ka pointer member const ho jaata hai, par jis cheez ko point kar raha hai woh nahi. (2) const_cast se compiler ko chup mat karao — agar object originally const declare hua tha to use modify karna undefined behavior hai. Aur agar cache ya counter jaisi cheez const method ke andar update karni ho, to use member ko mutable bana do — ye legal escape hatch hai.

Go deeper — visual, from zero

Test yourself — C++ Programming

Connections