Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

constants

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

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

What does it mean?

like image 592
aPoC Avatar asked Jun 29 '10 13:06

aPoC


People also ask

What does const at the end of a function declaration mean?

Declaring a member function with the const keyword specifies that the function is a "read-only" function that doesn't modify the object for which it's called. A constant member function can't modify any non-static data members or call any member functions that aren't constant.

Why do we use const at the end of a function header?

The const in the end means the function is constant, so it doesn't change the object's state. When you put the const in the end, you can't change the state of the object's members. Declaring a function static means it doesn't belong to the object at all, it belongs to the class type.

What does const mean in a function?

The const member functions are the functions which are declared as constant in the program. The object called by these functions cannot be modified. It is recommended to use const keyword so that accidental changes to object are avoided. A const member function can be called by any type of object.

What is const declaration?

A constant holds a value that does not change. A constant declaration specifies the name, data type, and value of the constant and allocates storage for it. The declaration can also impose the NOT NULL constraint.


2 Answers

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

like image 156
Janick Bernet Avatar answered Oct 05 '22 22:10

Janick Bernet


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.

like image 26
Nikolai Fetissov Avatar answered Oct 05 '22 22:10

Nikolai Fetissov