Skip to content
Theory · Classes and OOP · lesson 5 of 15

const methods

An object declared const cannot be changed. It is like an exhibit under glass: you can walk up and read its label, but you cannot rearrange anything inside. The analogy stops at who puts the glass there: not a museum but the declaration, so an object of the same class without const can be changed.

A class, though, has methods, and some only read the fields while others change them. Which of them may be called on an object under glass? The compiler decides by the method's declaration, not by its body:

C++
#include <iostream>

class Temperature {
public:
    Temperature(int celsius) : celsius_(celsius) {}
    int celsius() const { return celsius_; }
    void warm() { ++celsius_; }
private:
    int celsius_;
};

int main() {
    const Temperature t(20);
    std::cout << t.celsius() << '\n';  // 20
}

const methods

On a const object, as through a const T& reference, only const methods can be called. These are methods with the word const after the parameter list, like int celsius() const.

The word const here is a promise: the method does not change the object. The compiler holds it to that:

  • inside a const method the object is const too, and a field cannot be assigned there;
  • the exception is a field declared with the word mutable: it can be changed even in a const method. An example is a call counter, which does not change what the object means.

For const Temperature t(20);, the call t.celsius() compiles, while t.warm() does not: warm is declared without const, and the compiler does not let it near the exhibit.

That is why a method that only reads fields is declared const. It can be called on any object, const or not.

Why does it matter? Large objects are passed to functions by a const reference const T&, so they are not copied. Inside such a function, the object is the same exhibit under glass. If the reading methods are declared without const, the function cannot call even them.

Interviewers like to ask this: what const after the parameter list means. The answer: the method does not change the object it is called on.

Common mistake

A method only reads a field but is declared without const:

C++
class Temperature {
public:
    Temperature(int celsius) : celsius_(celsius) {}
    int celsius() { return celsius_; }

private:
    int celsius_;
};

int twice(const Temperature& t) {
    return t.celsius() * 2;  // ✗
}
TEXT
error: 'this' argument to member function 'celsius' has type 'const Temperature', but function is not marked const

The parameter t is a const reference, and celsius() does not promise to keep it unchanged, so the call does not compile. Add const after the parameter list: int celsius() const.

Key points

  • On a const object, and through const T&, only const methods can be called.
  • const after the parameter list promises that the method does not change the object.
  • The compiler checks the method's declaration, not its body.
  • Declare a method that only reads fields as const.

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way