digitalmars.D.learn - "new class" and accessing outer this
- Henning Hasemann <hhasemann web.de> Jun 18 2007
- Kirk McDonald <kirklin.mcdonald gmail.com> Jun 18 2007
- "Jarrett Billingsley" <kb3ctd2 yahoo.com> Jun 18 2007
- Henning Hasemann <hhasemann web.de> Jun 18 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
Jun 18 2007
Henning Hasemann wrote: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
Nested class instances have an .outer property, for example: class Something : Base { this() { dispatcher = new class Dispatcher { void foo() { this.outer.bar(); } }; } void bar() {} } -- Kirk McDonald http://kirkmcdonald.blogspot.com Pyd: Connecting D and Python http://pyd.dsource.org
Jun 18 2007
"Henning Hasemann" <hhasemann web.de> wrote in message news:20070618214023.53eb5058 amee...For those who don't know it: I'm talking here about the undocumented(?) new class { } - feature.
http://www.digitalmars.com/d/class.html#anonymous :)
Jun 18 2007
Am Mon, 18 Jun 2007 21:40:23 +0200 schrieb Henning Hasemann <hhasemann web.de>: Thanks to both answerers, that was exactly what I looked for. And sorry for my blindness ,-) 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
Jun 18 2007









Kirk McDonald <kirklin.mcdonald gmail.com> 