"new class" and accessing outer this

Henning Hasemann hhasemann at web.de
Mon Jun 18 12:40:23 PDT 2007


For those who don't know it: I'm talking here about the undocumented(?)
new class { } - feature. I.e. You can do the following:

class Foo {
 int bar() { return 5; }
}

auto x = new class Foo {
  int bar() { return 6; }
  int foo() { return 7; }
};

then x will hold an object of a class derived from Foo.


Say I have a contstruct like this:

class Dispatcher {
 // ...
}

class Base {
  Dispatcher dispatcher;
}

class Something : Base {
  this() {
    dispatcher = new class Dispatcher {
      void foo() {
        // XXX
      }
    };
  }
} // class Something


Now I want to access the "outer this" i.e. the instance of Something at
the line I marked with XXX. Is this possible without a temporary
variable that "renames" the outer this?
(simply 'this' would refer to the inner, i.e. the object foo is a
method of.)

Henning

-- 
GPG Public Key:
http://keyserver.ganneff.de:11371/pks/lookup?op=get&search=0xDDD6D36D41911851
Fingerprint: 344F 4072 F038 BB9E B35D  E6AB DDD6 D36D 4191 1851


More information about the Digitalmars-d-learn mailing list