What exactly does the const
keyword in C++ mean when it's written at the end of a member function (after the argument list)?
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.
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.
Putting const after a function declaration makes the function constant, meaning it cannot alter anything in the object that contains the function.
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).
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 ofthis
in a member function of a classX
isX*
. If the member function is declaredconst
, the type ofthis
isconst X*
. [section 9.3.2 §1]In a
const
member function, the object for which the function is called is accessed through aconst
access path; therefore, aconst
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With