[c++] What is meant with "const" at end of function declaration?

I got a book, where there is written something like:

class Foo 
{
public:
    int Bar(int random_arg) const
    {
        // code
    }
};

What does it mean?

This question is related to c++ constants

The answer is


Bar is guaranteed not to change the object it is being invoked on. See the section about const correctness in the C++ FAQ, for example.


Consider two class-typed variables:

class Boo { ... };

Boo b0;       // mutable object
const Boo b1; // non-mutable object

Now you are able to call any member function of Boo on b0, but only const-qualified member functions on b1.


Function can't change its parameters via the pointer/reference you gave it.

I go to this page every time I need to think about it:

http://www.parashift.com/c++-faq-lite/const-correctness.html

I believe there's also a good chapter in Meyers' "More Effective C++".


Similar to this question.

In essence it means that the method Bar will not modify non mutable member variables of Foo.


I always find it conceptually easier to think of that you are making the this pointer const (which is pretty much what it does).