C++ › Const Correctness

What is a const member function?

Beginner

Answer

A const member function (declared with const after the parameter list) promises not to modify the object's observable state and can be called on const objects. Inside it, member variables are treated as const (unless marked mutable). Marking read-only methods const is core to const-correctness and lets them be used on const instances.
struct Box {
    int size() const { return n; } // doesn't modify *this
    int n = 0;
};
Real-world example A getter is marked const so it can be called on a const Box passed to a function.

Common follow-ups: Can a const method modify members? | Why can const objects only call const methods?