Learning Hub Search Ctrl K

Legacy C++ › 2. Pointers, References and Memory

const Correctness

const objects, pointers, references, const member functions and mutable.

intermediate #const#mutable#const-member-functions#pointers

Explanation

const declares that something must not be modified, and the compiler enforces it. With pointers, read the declaration right to left:

Declaration Meaning
const int* p / int const* p pointer to a const int: cannot change *p, can reseat p
int* const p const pointer to int: can change *p, cannot reseat p
const int* const p neither can change

A const member function (int size() const) promises not to modify the object. Only const member functions can be called on a const object or through a const&. A member declared mutable may still change inside a const function (for caches, mutexes, counters).

Example

#include <iostream>
#include <string>

class Account {
    std::string owner_;
    double      balance_;
    mutable int reads_;              // bookkeeping, not logical state
public:
    Account(const std::string& o, double b) : owner_(o), balance_(b), reads_(0) {}

    double balance() const { ++reads_; return balance_; }   // const: observes only
    void   deposit(double amt) { balance_ += amt; }         // non-const: modifies
    const std::string& owner() const { return owner_; }     // no copy, no modification
    int    reads() const { return reads_; }
};

void print(const Account& a) {          // promise not to change the argument
    std::cout << a.owner() << ": " << a.balance() << "\n";
    // a.deposit(1);                    // error: deposit is not const
}

int main() {
    Account acc("Ann", 100);
    acc.deposit(50);
    print(acc);

    const int limit = 10;               // usable as an array size in C++
    int table[limit];
    (void)table;

    const char* msg = "fixed text";     // pointer to const data
    // msg[0] = 'F';                    // error
    std::cout << msg << " " << acc.reads() << "\n";
    return 0;
}

Common pitfalls

  • Forgetting const on getters makes them unusable on const objects and const& parameters.
  • const_cast to remove constness and then writing to a truly const object is undefined behavior.
  • Const-ness is viral: add it from the start; retrofitting it forces changes through many call chains.
  • Returning a non-const reference from a const member function (int& get() const) defeats the guarantee.
  • mutable is for state that is not part of the object’s logical value; do not use it to silence errors.

Legacy vs modern

C++11 adds constexpr (compile-time constants/functions); const itself works the same way. In C++11 const member functions are also expected to be thread-safe.

Exercises

1. What is the type of p in int const* const p = &x;?

A const pointer to a const int; neither the pointer nor the pointee can change through p.

2. Why can print(const Account&) call balance() but not deposit()?

balance() is declared const; deposit() is not, so it is not callable on a const reference.