-1

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

Chumanista
  • 92
  • 12

3 Answers3

1

It means the function (operator) does not change the object.

wallyk
  • 55,472
  • 16
  • 84
  • 144
1

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.

MrDuk
  • 14,538
  • 15
  • 62
  • 127
  • Herb Sutter's GotW articles are quite good, too. See [GotW #6](http://gotw.ca/gotw/006.htm). – Void Apr 17 '14 at 20:06
1

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.

Ben
  • 1,757
  • 18
  • 29