- Back to Home »
- Mutable keyword
Posted by : Sushanth
Wednesday, 16 December 2015
The mutable storage class specifier is used only on a class data member to make it modifiable even though the member is part
of an object declared as const.
of an object declared as const.
Cant be used with names declared as static or const, or reference members.
Example:
class A
{
public:
A() : x(4), y(5) { };
mutable int x;
int y;
};
int main()
{
const A var2;
var2.x = 345;
// var2.y = 2345;
}
the compiler would not allow the assignment var2.y = 2345 because var2 has been declared as const. The compiler will allow the assignment var2.x = 345 because A::x has been declared as mutable.
{
const A var2;
var2.x = 345;
// var2.y = 2345;
}
the compiler would not allow the assignment var2.y = 2345 because var2 has been declared as const. The compiler will allow the assignment var2.x = 345 because A::x has been declared as mutable.