0

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

Chumanista
  • 102
  • 12

3 Answers3

1

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

wallyk
  • 56,922
  • 16
  • 83
  • 148
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
  • 16,578
  • 18
  • 74
  • 133
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,816
  • 1
  • 20
  • 29