Paul said:
Why would you make such a function const in the first place?
If its not const why mark it as const?
A practical example where you need to modify the mutable member variable
of a const object is a reference-counting smart pointer which uses double
linking for the reference counting.
Such a smart pointer, instead of actually using an actual integral
reference count, has a 'prev' and 'next' pointer as members, like a
doubly-linked list. Whenever two smart pointers point to the same object
(in which case the "reference count" would be larger than 1), they are
linked together with those pointers (possibly via a larger chain of
smart pointer objects, if there are more than two pointing to the same
object). The advantage of this is that you don't need to allocate a
separate reference count (which is more efficient), nor require one to
be in the managed object (which means the smart pointer is not intrusive
and thus works with any type). The disadvantage is that the size of the
smart pointer is that of three pointers.
The 'prev' and 'next' pointers have to be mutable. There's no way around
it. That's because the copy constructor and assignment operators must take
the parameter as const:
TehSmartPointer::TehSmartPointer(const TehSmartPointer& rhs):
object(rhs.object)
{
// Insert 'this' into the linked list where 'rhs' is. It has to modify
// 'rhs.prev' and 'rhs.prev->next', which have to be mutable.
}