const correctness — const variables, const pointers, const member functions
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 | ❌ | ❌ |

3. const member functions
WHAT does const do to this?
- In a non-const method,
thishas typeCircle*(or reallyCircle* const). - In a const method,
thisbecomesconst 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
thishave inside a const method? →const ClassName*. - Which methods can you call on a
constobject? → Onlyconstmember 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?
Why must a const variable be initialized immediately?
How do you read const int* const p?
In const int* p, what is protected?
*p = ..., but you CAN repoint p.In int* const p, what is protected?
p, but you CAN do *p = ....What does const after a member function's parameter list promise?
this becomes a const ClassName*.Which member functions can be called on a const object?
const.What does mutable do?
Is C++ const-ness deep or shallow?
Why is const_cast-then-modify on an originally-const object dangerous?
What's the rule for which side const binds to?
Why prefer const int x over #define X?
Connections
- Pointers and References in C++
- constexpr and compile-time evaluation
- Function parameters — pass by value vs const reference
- Classes and the this pointer
- Operator overloading — const operator[]
- Undefined Behavior in C++
- mutable and logical constness
Concept Map
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.