const pointer vs pointer to const, and const correctness

Difficulty: Beginner

Question

Explain the difference between 'const int* p', 'int* const p' and 'const int* const p'. What is const correctness and what does a const member function mean?

Answer

If you have ever stared at a declaration like 'const int* const p' and felt your eyes glaze over, you are in good company. The good news is there is a mechanical trick that makes all of them easy, and interviewers love checking whether you know it.

The trick is to read the declaration right to left, or more precisely: whatever is immediately to the left of the const is what is constant (and if const is the leftmost token, it applies to the thing right after it). So 'const int* p' reads as 'p is a pointer to an int that is const': you cannot modify the int through p, but you can point p somewhere else. 'int const* p' means exactly the same thing. Now 'int* const p' reads 'p is a const pointer to an int': p itself is fixed for life and must be initialised, but you can modify the int it points at. And 'const int* const p' is both: neither the pointee nor the pointer can change.

An everyday analogy: imagine a hotel key card. A pointer to const is a card that lets you look into a room but not rearrange it, although you can swap the card for a different room's card. A const pointer is a card permanently tied to one room, where you may rearrange the furniture. The last form is a permanent view-only card.

Const correctness is the discipline of marking everything that should not change as const, so the compiler enforces your intent. It matters because it documents contracts (this function will not touch your data), enables binding temporaries to const references, allows the compiler to catch bugs early, and lets const objects be used at all: you can only call const member functions on a const object.

A const member function, declared as 'int size() const', promises not to modify the object's observable state: inside it, 'this' has type 'const T*', so assigning to normal data members is a compile error. Getters should almost always be const. If a member genuinely needs to change inside a const function (a cache or a mutex), mark that member mutable. Overloading on constness is also legal and used by containers: operator[] has one version returning T& and a const version returning const T&.

Edge cases interviewers probe: const_cast can strip constness, but modifying an object that was originally defined const through it is undefined behaviour. Also, a top-level const on a by-value parameter ('void f(const int x)') is not part of the function signature, whereas const on the pointee ('const int') is. And a string literal has type const char[N] in C++, so 'char s = "hi"' is ill-formed.

Code examples

Three flavours of const with pointers

#include <iostream>

class Counter {
    int n_ = 0;
    mutable int reads_ = 0;
public:
    void inc() { ++n_; }
    int get() const { ++reads_; return n_; }  // reads_ is mutable
    int reads() const { return reads_; }
};

int main() {
    int a = 10, b = 20;

    const int* p1 = &a;      // pointer to const int
    p1 = &b;                 // OK: pointer can move
    // *p1 = 5;             // error: cannot modify through p1

    int* const p2 = &a;      // const pointer to int
    *p2 = 5;                 // OK: modify pointee
    // p2 = &b;             // error: pointer is fixed

    const int* const p3 = &a;  // neither can change
    std::cout << *p1 << " " << *p2 << " " << *p3 << "\n";

    const Counter c;
    std::cout << c.get() << " " << c.reads() << "\n";
}

p1 now points to b (20). a was changed to 5 through p2. On the const Counter, only const member functions are callable; the mutable counter reads_ still updates.

Key points

Common follow-up questions

Interview tips

Concepts covered

const, pointers, const member functions, const correctness

More C++ & STL questions