mutable keyword

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri May 20 13:46:18 PDT 2016


On Friday, May 20, 2016 18:23:26 Jack Applegame via Digitalmars-d-learn wrote:
> On Friday, 20 May 2016 at 17:28:55 UTC, Namespace wrote:
> > But you can cheat:
> You can just cast const away:
> struct A {
>   int id = 0;
>
>   this(int id) {
>       this.id = id;
>   }
>
>   void change() const {
>       (cast() id)++;
>   }
> }
>
> void main() {
>   import std.stdio;
>
>   A a = A(42);
>   a.change();
>
>   writeln(a.id);
> }

Casting away const and mutating is undefined behavior in D. No D program
should ever do it.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list