"new class" and accessing outer this
Kirk McDonald
kirklin.mcdonald at gmail.com
Mon Jun 18 12:55:39 PDT 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
More information about the Digitalmars-d-learn
mailing list