Can't modify this

Ary Borenszweig via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jun 28 13:40:19 PDT 2014


This doesn't work:

class Foo {
   this() {
     this = new Foo;
   }
}

Error: Cannot modify 'this'

However you can do this:

class Foo {
   this() {
     auto p = &this;
     *p = new Foo();
   }
}

It even changes the value of this!

Should that compile? I mean, it's the same as modifying 'this'...


More information about the Digitalmars-d-learn mailing list