Possible Duplicates:
What's the use of const here
Using 'const' in class's functions
Hi All,
I keep making mistakes about the use of const with class methods and variables. For example, sometimes I fix problems using
const int myfunc(const int &obj) const { }
some other times I feel I don’t need const at the end since the parameter is already const, so I don’t see why I should enforce this fact by appending a const at the end.
const int myfunc(const int &obj) const { }constindicates that the return value is constant. In this particular case, it’s not particularly relevant since theintis a value as opposed to a reference.constindicates the parameterobjis constant. This indicates to the caller that the parameter will not be modified by the function.constindicates the functionmyfuncis constant. This indicates to the caller that the function will not modify non-mutable member variables of the class to which the function belongs.Regarding #3, consider the following: