Thread overview | ||||||
---|---|---|---|---|---|---|
|
February 19, 2006 Getting 'this' of outer class? | ||||
---|---|---|---|---|
| ||||
I'm sort of new to the inner class concept. I know the inner class has access to the outer class through a 'hidden' this reference, but how do I access this reference directly? Say I have the following: # class A # { # class B # { # B getB() { return this; } // Ok # A getA() { return ???; } // How do I do this? # } # } Right now I'm stuck with adding a private getThis() member to A... Nick |
February 19, 2006 Re: Getting 'this' of outer class? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick | "Nick" <Nick_member@pathlink.com> wrote in message news:dta0ca$2d3p$1@digitaldaemon.com... > I'm sort of new to the inner class concept. I know the inner class has > access to > the outer class through a 'hidden' this reference, but how do I access > this > reference directly? Say I have the following: > > # class A > # { > # class B > # { > # B getB() { return this; } // Ok > # A getA() { return ???; } // How do I do this? > # } > # } > > Right now I'm stuck with adding a private getThis() member to A... I don't think you can, not in the current implementation of inner classes, at least. There's probably a way to hack the inner class reference to get at the outer class reference, but it's probably not recommended ;) I agree that it'd be very nice to have. It's kind of like not being able to get the context pointer of delegates (which, according to Walter, are similar to how inner classes are implemented). |
February 20, 2006 Re: Getting 'this' of outer class? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick | Nick wrote:
> I'm sort of new to the inner class concept. I know the inner class has access to
> the outer class through a 'hidden' this reference, but how do I access this
> reference directly? Say I have the following:
>
> # class A
> # {
> # class B
> # {
> # B getB() { return this; } // Ok
> # A getA() { return ???; } // How do I do this?
> # }
> # }
>
> Right now I'm stuck with adding a private getThis() member to A...
>
> Nick
>
>
Which reference do you want?
Any number of A objects can create any B objects, unless A is a singleton what object would you be calling getThis() from??
How about:
class B {
A parent;
this( A parent ) { this.parent = parent; }
...
}
-DavidM
|
September 16, 2006 Re: Getting 'this' of outer class? | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Medlock | To get the object reference of an outer class from an inner class: class A { class B { B getB {return this;} A getA {return A.this;} //=======// } } |
Copyright © 1999-2021 by the D Language Foundation