Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of a const at end of a member function? [duplicate]

Tags:

c++

constants

What exactly does the const keyword in C++ mean when it's written at the end of a member function (after the argument list)?

like image 245
Mat Avatar asked Oct 30 '10 17:10

Mat


People also ask

What does const mean in 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 does putting const after a function mean C++?

A function becomes const when the const keyword is used in the function's declaration. The idea of const functions is not to allow them to modify the object on which they are called. It is recommended the practice to make as many functions const as possible so that accidental changes to objects are avoided.

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

Putting const after a function declaration makes the function constant, meaning it cannot alter anything in the object that contains the function.

What does making a member function const do?

A const member function is a member function that guarantees it will not modify the object or call any non-const member functions (as they may modify the object).


2 Answers

It means that *this is const inside that member function, i.e. it doesn't alter the object.

The keyword this is a prvalue expression whose value is the address of the object for which the function is called. The type of this in a member function of a class X is X*. If the member function is declared const, the type of this is const X*. [section 9.3.2 §1]

In a const member function, the object for which the function is called is accessed through a const access path; therefore, a const member function shall not modify the object and its non-static data members. [section 9.3.2 §2]

This means that a const member function can be called on a const instance of the class. A non-const member function can't be called on [1]a const object, since it could potentially try to modify it.

[1] Note: a temporary is not a const object unless it's of const type.

like image 103
Oliver Charlesworth Avatar answered Sep 22 '22 10:09

Oliver Charlesworth


const at the end of a function signature means that the function should assume the object of which it is a member is const. In practical terms it means that you ask the compiler to check that the member function does not change the object data in any way. It means asking the compiler to check that it doesn't directly change any member data, and it doesn't call any function that itself does not guarantee that it won't change the object.

When you create a const object you are asking the compiler to make sure that that object does not change beyond its initialization. That in turns means that the compiler will check you don't directly change its member data and that you don't call any function that does not guarantee it won't change the object.

This is all part of the const correctness philosophy. In essence it means that if things work right now and they won't change then they will never break. In other words, constant things are easier to work with reliably. This const thing at the end of function signatures is a tool for you to prohibit things from breaking. This in turns means you should put const everywhere you possibly can.

like image 37
wilhelmtell Avatar answered Sep 23 '22 10:09

wilhelmtell