What is meant with "const" at end of function declaration?

C++Constants

C++ Problem Overview


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

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

What does it mean?

C++ Solutions


Solution 1 - C++

A "const function", denoted with the keyword const after a function declaration, makes it a compiler error for this class function to change a member variable of the class. However, reading of a class variables is okay inside of the function, but writing inside of this function will generate a compiler error.

Another way of thinking about such "const function" is by viewing a class function as a normal function taking an implicit this pointer. So a method int Foo::Bar(int random_arg) (without the const at the end) results in a function like int Foo_Bar(Foo* this, int random_arg), and a call such as Foo f; f.Bar(4) will internally correspond to something like Foo f; Foo_Bar(&f, 4). Now adding the const at the end (int Foo::Bar(int random_arg) const) can then be understood as a declaration with a const this pointer: int Foo_Bar(const Foo* this, int random_arg). Since the type of this in such case is const, no modifications of member variables are possible.

It is possible to loosen the "const function" restriction of not allowing the function to write to any variable of a class. To allow some of the variables to be writable even when the function is marked as a "const function", these class variables are marked with the keyword mutable. Thus, if a class variable is marked as mutable, and a "const function" writes to this variable then the code will compile cleanly and the variable is possible to change. (C++11)

As usual when dealing with the const keyword, changing the location of the const key word in a C++ statement has entirely different meanings. The above usage of const only applies when adding const to the end of the function declaration after the parenthesis.

const is a highly overused qualifier in C++: the syntax and ordering is often not straightforward in combination with pointers. Some readings about const correctness and the const keyword:

Const correctness

The C++ 'const' Declaration: Why & How

Solution 2 - C++

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.

Solution 3 - C++

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.

Solution 4 - C++

Similar to this question.

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

Solution 5 - C++

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

Solution 6 - C++

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++".

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionaPoCView Question on Stackoverflow
Solution 1 - C++Janick BernetView Answer on Stackoverflow
Solution 2 - C++Nikolai FetissovView Answer on Stackoverflow
Solution 3 - C++mkluweView Answer on Stackoverflow
Solution 4 - C++John WarlowView Answer on Stackoverflow
Solution 5 - C++GozView Answer on Stackoverflow
Solution 6 - C++orbfishView Answer on Stackoverflow