I understand that the keyword const marks variables, parameters etc as read only. In a class declaration why is const used at the end of a member declaration, for example:
QString getClassName() const;
Declaring a method const
means that the method won't change the state of the object and thus it can be called on const
instances of the class.
Notice that const
-ness is enforced by the compiler. const
methods can't modify member variables unless they are declared mutable
. It's very common to have mutable
locks so that const
methods can still be synchronized.
It basically means that the function is "promising" that it won't change the calling object.