What Is Const Correctness in C++?
Learn what const correctness means in C++, how const parameters and member functions work, and why it prevents accidental mutation bugs today.
Expected Interview Answer
Const correctness means marking variables, function parameters, and member functions as const wherever they aren't meant to be modified, so the compiler enforces read-only intent at compile time and catches accidental mutation as an error.
A const variable can't be reassigned after initialization. A const reference or pointer parameter (e.g., `void print(const std::string& s)`) promises the function won't modify the caller's object, which also allows temporaries and literals to bind to it. A const member function (declared `void foo() const`) promises it won't modify the object's non-mutable members, and only const member functions can be called on a const object or through a const reference/pointer. Const correctness cascades through a codebase: once one function takes a const reference, everything it calls on that reference must also be const-correct, which is why it's easier to design const-correct from the start than to retrofit it later. The `mutable` keyword offers an escape hatch for fields (like a cache or mutex) that need to change even inside a logically const method.
- Lets the compiler catch accidental mutation as a compile error
- Documents intent directly in function signatures
- Enables calling functions safely on const objects/references
- Allows temporaries and literals to bind to const reference parameters
- Improves optimizer opportunities by asserting non-mutation
AI Mentor Explanation
Const correctness is like handing a visiting analyst a laminated copy of the team's playbook marked 'read-only' — they can study every page and quote it in their report, but the plastic lamination physically prevents them from scribbling changes into the actual playbook. A coach handing out an unlaminated original invites someone to accidentally alter a strategy page mid-review.
Step-by-Step Explanation
Step 1
Mark non-modified parameters const
Pass large objects by const reference (e.g., const std::string&) to avoid copies while promising no mutation.
Step 2
Mark member functions const
Add const after a member function's parameter list if it doesn't modify the object's state.
Step 3
Enable calling on const objects
Only const member functions can be invoked on a const object or through a const reference/pointer to it.
Step 4
Use mutable for true exceptions
Fields that must change even in a const method (like an internal cache or mutex) can be marked mutable.
Step 5
Let const cascade through the call chain
Every function called from within a const context on the same object must itself be const-correct.
What Interviewer Expects
- Explains const on variables, parameters, and member functions distinctly
- Knows only const member functions can run on const objects
- Mentions the mutable keyword as the deliberate escape hatch
- Understands const correctness must cascade through called functions
- Can identify a compile error caused by a const-correctness violation
Common Mistakes
- Forgetting to mark a getter method const, breaking calls on const objects
- Confusing 'const pointer' with 'pointer to const' (right-to-left binding)
- Overusing mutable to silence errors instead of fixing real design issues
- Passing large objects by value instead of by const reference
Best Answer (HR Friendly)
“Const correctness means marking anything that shouldn't change — variables, parameters, or member functions — as const, so the compiler enforces that promise and catches accidental mutations before the code even runs, which makes intent clear and prevents a whole class of bugs.”
Code Example
class Account {
double balance;
public:
// const parameter: won't modify the passed string
void setOwner(const std::string& name) { owner = name; }
// const member function: won't modify *this
double getBalance() const { return balance; }
private:
std::string owner;
};
void printBalance(const Account& acc) {
std::cout << acc.getBalance(); // only const methods callable here
}Follow-up Questions
- What is the difference between a const pointer and a pointer to const data?
- When would you use the mutable keyword, and why is it risky if overused?
- How does const correctness interact with function overload resolution?
- What is a const_cast and when, if ever, is it appropriate to use?
- How does const correctness relate to writing exception-safe code?
MCQ Practice
1. What does marking a member function const promise?
A const member function promises not to modify the object it's called on, aside from mutable members.
2. Which objects can call only const member functions?
Only const member functions are callable through a const object, reference, or pointer.
3. What keyword allows a member to change even inside a const method?
mutable is the deliberate escape hatch for fields like caches that must change even in logically const methods.
Flash Cards
Const correctness — Marking things that shouldn't change as const so the compiler enforces it.
Const member function — Promises not to modify the object's state; callable on const objects.
mutable keyword — Lets a specific member change even inside a const method.
Const reference parameter benefit — Avoids copies while guaranteeing the caller's object isn't modified.