Const Correctness
CONCEPTS:C++ Const Correctness
Question Variations
- "Why should getters usually be marked `const`?"
- "What is the difference between `const int*` and `int* const`?"
- "Can a const object call a non-const member function?"
- "When is `mutable` appropriate?"
Why This Is Asked
Const correctness is a core C++ API-design tool. This question tests whether you can express read-only intent, understand how const affects member functions and pointer declarations, and avoid weakening type guarantees with unnecessary casts.
Key Concepts
- A
constmember function cannot modify non-mutable members throughthis. - Const and non-const overloads can return appropriately qualified references.
const T*,T* const, andconst T* constqualify different parts of a pointer declaration.constprevents modification through that access path; it does not necessarily mean the underlying object is immutable.
Question Variations
- “Why should getters usually be marked
const?” - “What is the difference between
const int*andint* const?” - “Can a const object call a non-const member function?”
- “When is
mutableappropriate?”