What is the const Keyword in C++?
Learn what the const keyword does in C++, the difference between const pointers and pointers to const, and how const member functions enforce immutability.
Expected Interview Answer
The const keyword in C++ marks something as read-only, telling the compiler its value must not change after initialization and enforcing that promise at compile time.
const can qualify variables, pointers, function parameters, return values, and member functions. A const member function promises not to modify the object's observable state, so it can be called on const objects. Because const is checked at compile time, it catches accidental mutations, enables optimizations, and documents intent — it has no runtime cost. Note the difference between const pointers (the pointer is fixed) and pointers to const (the pointee is read-only).
- Prevents accidental modification of data
- Documents intent and improves readability
- Enables compiler optimizations
- Allows functions to accept read-only references cheaply
- Lets member functions be called on const objects
AI Mentor Explanation
A const value is like the boundary rope fixed around a cricket ground before play begins: once it is laid out for the match, no fielder or umpire may shift it mid-over. Everyone can look at it and measure distances from it, but any attempt to move it is refused before a ball is even bowled — just as the compiler rejects any code that tries to change a const.
Step-by-Step Explanation
Step 1
Declare a const variable
Write const int MAX = 100; the value must be initialized here and can never be reassigned.
Step 2
Distinguish pointer forms
const int* p means the pointee is read-only; int* const p means the pointer itself is fixed; const int* const p locks both.
Step 3
Use const parameters
Pass large objects as const T& to avoid copies while promising not to mutate the caller's data.
Step 4
Add const member functions
Append const after the signature (int size() const) so the method promises not to modify the object and can run on const instances.
Step 5
Return const where useful
Return const references to expose internal data for reading without allowing external modification.
What Interviewer Expects
- Clear definition of const as compile-time immutability
- Difference between const pointer and pointer to const
- Understanding of const member functions
- Why const references avoid copies
- Awareness that const is enforced at compile time with no runtime cost
Common Mistakes
- Confusing const int* with int* const
- Thinking const guarantees runtime security rather than compile-time checking
- Forgetting to mark read-only member functions const
- Believing const adds runtime overhead
- Trying to modify a const via a non-const alias without understanding const_cast dangers
Best Answer (HR Friendly)
“The const keyword tells C++ that a value should never change after it is first set. It helps prevent accidental mistakes, makes the code easier to understand, and lets the compiler catch bugs before the program even runs.”
Code Example
#include <iostream>
class Circle {
double radius;
public:
Circle(double r) : radius(r) {}
// const member function: promises not to modify the object
double area() const { return 3.14159 * radius * radius; }
};
int main() {
const int MAX = 100; // read-only value
// MAX = 200; // error: assignment of read-only variable
int x = 5, y = 9;
const int* p = &x; // pointer to const: *p is read-only
p = &y; // ok: pointer can move
// *p = 7; // error: cannot modify pointee
int* const q = &x; // const pointer: q is fixed
*q = 7; // ok: pointee is writable
// q = &y; // error: cannot move pointer
const Circle c(2.0);
std::cout << c.area() << '\n'; // ok: area() is const
return 0;
}Follow-up Questions
- What is the difference between const int* and int* const?
- What does a const member function guarantee?
- When would you use const_cast and why is it risky?
- How does constexpr differ from const?
- Why pass parameters as const references?
MCQ Practice
1. What does const int* p declare?
const int* p is a pointer to const int: the pointee cannot be modified through p, but p itself can be reassigned.
2. A const member function is allowed to:
A const member function promises not to modify the object's observable state, so it can read members and be called on const objects.
3. When is a const violation detected in C++?
const-correctness is enforced by the compiler, so attempts to modify a const are rejected at compile time with no runtime cost.
Flash Cards
What does const mean in C++? — It marks a value as read-only, enforced at compile time so it cannot be modified after initialization.
const int* vs int* const? — const int* is a pointer to const (pointee read-only); int* const is a const pointer (pointer fixed, pointee writable).
What is a const member function? — A method with const after its signature that promises not to modify the object and can be called on const instances.
Does const add runtime cost? — No. const is a compile-time contract; it enables optimizations rather than adding overhead.