How to break const

Jonathan M Davis jmdavisProg at gmx.com
Sun Jun 17 23:01:49 PDT 2012


On Monday, June 18, 2012 07:36:26 Mehrdad wrote:
> Is it just me, or did I subvert the type system here?
> 
> 
> import std.stdio;
> 
> struct Const
> {
> 	this(void delegate() increment)
> 	{ this.increment = increment; }
> 	int a;
> 	void delegate() increment;
> 	void oops() const { this.increment(); }
> }
> 
> void main()
> {
> 	Const c;
> 	c = Const({ c.a++; });
> 	writeln(c.a);
> 	c.oops();
> 	writeln(c.a);
> }

No, actually I don't think that you did. The delegate isn't accessing the this 
pointer - which is where const would kick in. It's accessing c through a non-
const reference to the data that was put in its body inside of main.

- Jonathan M Davis


More information about the Digitalmars-d mailing list