mutable keyword

ciechowoj via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri May 20 11:42:14 PDT 2016


On Friday, 20 May 2016 at 18:23:26 UTC, Jack Applegame 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);
> }


Is it valid code (I'm asking about undefined behavior)?



More information about the Digitalmars-d-learn mailing list