In C++ you can declare class methods as
const
const
The idea is nice, since it allows the compiler to detect a probably undesired changes to data. But as with almost every nice feature of C++, there are times when you need a way to circumvent it, because otherwise it prevents you from doing what you need.
Imagine, for example, a proprietary collection, which has a synchronized "get" operation. In the function it needs to lock some internal data structures to ensure data integrity and then returns the result. The problem is, that to lock using some C++ locking objects usually means breaking the
const
Another example - consider using logging in your
const
That's where we see another C++ trick - to work around this problem there is something called
const_cast<T>
Another option is to use the
mutable
const
The more I see things like that in conventional programming languages, the more I like functional programming languages, such as ML and Haskell. In their core they don't even have a notion of a "variable" - something that can change a value. All functions are
const