I understand that the function is not allowed to change the state of the object, but I thought I read somewhere that the compiler was allowed to assume that if the function
Corey is correct, but bear in mind that any member variables that are marked as mutable can be modified in const member functions.
It also means that these functions can be called from other const functions, or via other const references.
Edit: Damn, was beaten by 9 seconds.... 9!!! :)
The const keyword on a member function marks the this parameter as constant. The function can still mute global data (so can't be cached), but not object data (allowing for calls on const objects).
const methods are also allowed to modify static locals. For example, the following is perfectly legal (and repeated calls to bar() will return increasing values - not a cached 0):
class Foo
{
public:
int bar() const
{
static int x = 0;
return x++;
}
};