A const member function in C++ is a member function declared with const after its parameter list. It promises not to modify the object it is called on, so it can be called on const objects, const references and const pointers. Marking every function that only reads state as const is one of the basic habits of correct C++ class design.
The promise is enforced by the compiler: inside a const member function, this has the type const T*, so assigning to an ordinary data member is a compile error. This guide explains the syntax, what the compiler checks, how const and non-const overloads work, and where the promise has limits.
Put const after the closing parenthesis of the parameter list, in the declaration and again in the definition if you define it outside the class.
The object a is const, so only const member functions can be called on it. Calling deposit is rejected at compile time, which is exactly the safety the keyword provides.
In a non-const member function of class Account, this is Account*. In a const member function it is const Account*. Every data member is therefore treated as const inside the body.
Two consequences are worth remembering:
const is part of a member function's signature, so you can provide two versions: one for const objects and one for mutable objects. The compiler picks the const version when the object is const.
This is the pattern used by std::vector::operator[] and std::map::at. When both overloads share a long body, call one from the other with a cast, or in C++23 use an explicit object parameter (this auto&&) to write the function once.
Most functions take objects as const T&. If the class has no const member functions, the caller cannot do anything useful with that reference. This is why const correctness spreads: once one function takes const Account&, the members it calls must be const.
| Object type | Can call const member | Can call non-const member |
|---|---|---|
Account | yes | yes |
const Account | yes | no |
const Account& | yes | no |
Account* const (const pointer) | yes | yes |
const Account* | yes | no |
The last two rows differ: Account* const makes the pointer const, while const Account* makes the pointee const. Only the latter blocks mutating calls.
The promise is about the members of this object, not about everything reachable from it.
int* p_ becomes int* const, not const int*.mutable can be modified, which is covered in a related article.const_cast can remove const, but modifying an object that was originally defined const is undefined behaviour.This compiles because the const function does not change the pointer, only what it points at. If the pointee is owned state, consider std::unique_ptr or a wrapper such as the library's std::experimental::propagate_const idea to carry constness through.
Constructors and destructors cannot be const: they must be able to initialise and destroy the object. Static member functions cannot be const either, because they have no this. A const object is considered const only after its constructor finishes and until its destructor starts.
Yes. A getter that does not change the object should be const, so it works on const objects and through const& parameters.
Yes. Constness applies to the object's members, not to local variables or parameters of the function.
Yes, for member functions. T& get() and const T& get() const are different overloads. You cannot overload free functions on top-level const of a by-value parameter.
No by itself. The standard library assumes const member functions are safe to call concurrently, so your own const functions should follow that rule, which means any mutable state needs its own synchronisation.
Yes. The type of &Account::balance is double (Account::*)() const, and it differs from a non-const member function pointer type.
A const member function declares const after the parameter list, receives this as a pointer to const and can be called on const objects. Use it for every function that only reads state, provide const and non-const overloads when access differs, and remember that pointer members and mutable fields are not protected by the promise.