From my understanding , mutable
cancels the constness
of a variable
Class A {
void foo() const {
m_a = 5;
}
mutable int m_a;
};
>
Simply put, declaring a member variable as mutable
makes it write-accessible from any constant method of that class without any other special syntax. const_cast
on the other hand has to be performed whenever you want write access to an otherwise constant variable, and that variable doesn't even have to be a class member.
Unless you want to explicitly allow write access to a member variable, using const_cast
in every case of violation of const correctness is preferred, if only to clearly state your intentions.
On a side note, const_cast can also be used to add or remove the volatile
modifier.