You can use mutable for variables that can be changed in instances of const objects. This is called a logical constant (the opposite of a bit constant), since the object has not changed from a user point of view.
You can, for example, cache line lengths to improve performance.
class MyString
{
public:
...
const size_t getLength() const
{
if(!m_isLenghtCached)
{
m_length = doGetLength();
m_isLengthCached = true;
}
return m_length;
}
private:
sizet_t doGetLength() const { }
mutable size_t m_length;
mutable bool m_isLengthCached;
};