I saw following code on c++ reference:
bool operator() (const int& lhs, const int&rhs) const
What does the last const do?

- 102
- 12
3 Answers
From the Const Correctness tutorial:
If you have a const object, you don't want to call methods that can change the object, so you need a way of letting the compiler know which methods can be safely called. These methods are called "const functions", and are the only functions that can be called on a const object. Note, by the way, that only member methods make sense as const methods. Remember that in C++, every method of an object receives an implicit this pointer to the object; const methods effectively receive a const this pointer.
It might be worthwhile (spoiler: it is) to read through the whole article if you're new to the concept of constness.

- 16,578
- 18
- 74
- 133
-
Herb Sutter's GotW articles are quite good, too. See [GotW #6](http://gotw.ca/gotw/006.htm). – Void - Othman Apr 17 '14 at 20:06
Effectively makes the "this" pointer a pointer to a const object. It means that members of the object cannot be modified in that method, nor can that method be invoked on a non-const object.

- 1,816
- 1
- 20
- 29