C++ to D: mutable

Kagamin spam at here.lot
Fri Mar 25 01:28:00 PDT 2011


Jonathan M Davis Wrote:

> > const int a=0;
> > *cast(int*)&a=1;
> 
> There are so many reasons to cringe at that. Taking the address of a local 
> variable is generally very dangerous. As long as the pointer doesn't escape 
> and exist beyond the life the variable, then you're okay, but you often can't 
> guarantee that, and it's generally a _bad_ thing to do.

`mutable` is a modifier for member fields, I suppose.
You see the code. If it has bugs, you can find them.

> It breaks the type system. It's 
> also _very_ bad to do in the general case, because the variable in question 
> could actually be immutable underneath rather than just const.

Can you create an immutable object of a class without immutable constructor?


More information about the Digitalmars-d-learn mailing list