const int myfunc(const int &obj) const { }
- The first
const
indicates that the return value is constant. In this particular case, it's not particularly relevant since the int
is a value as opposed to a reference.
- The second
const
indicates the parameter obj
is constant. This indicates to the caller that the parameter will not be modified by the function.
- The third
const
indicates the function myfunc
is 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:
class MyClass
{
void Func1() { ... } // non-const member function
void Func2() const { ... } // const member function
};
MyClass c1; // non-const object
const MyClass c2; // const object
c1.Func1(); // fine...non-const function on non-const object
c1.Func2(); // fine... const function on non-const object
c2.Func1(); // oops...non-const function on const object (compiler error)
c2.Func2(); // fine... const function on const object