Re: Question on Basic Function Syntax
Jorgen Grahn <email@example.com> wrote:
> Shouldn't modify the object /via the 'this' pointer/ to be more
As said, the 'mutable' keyword allows modifications of the object pointed
by 'this' even if it's const. (While there are other ways to bypass constness,
'mutable' is by far the cleanest and most self-documenting way of doing it.
Basically you are telling at a language level that "this member variable can
be modified by const methods.")
One concrete example of a valid use for 'mutable' is a non-intrusive
smart pointer that uses the double-linking strategy (instead of using a
reference counter): When such a smart pointer is copied/assigned, the
original (which is passed as const reference to the copy constructor or
copy assignment operator) is not modified in its behavior, but internally
its "linded list" pointers need to be modified, and marking them as 'mutable'
is the cleanest way of doing that. (The external behavior of the pointer still
retains its constness, so this is a valid situation to use 'mutable'.)