I just came across this code fragment, but I do not understand how it compiles:
class temp {
int value1;
mutable int value2;
public:
void fun(int val) const
{
((temp*) this)->value1 = 10;
value2 = 10;
}
};
What is the meaning of this line
((temp*) this)->value1 = 10;
value1
is getting assigned to 10, without any error. But value1
is not mutable
. How does this compile?
const temp *
atemp *
. It is also outright hideous.const temp a; a.fun(4);
, then the line you quotes would be modifying an actually const variable. The assignment to value2 is not because value2 has been declared asmutable
(and doesn't need nasty C-style or const_cast to make it work...)