Accessing this of containing class

Jonathan M Davis jmdavisProg at gmx.com
Thu Feb 3 00:43:43 PST 2011


On Thursday 03 February 2011 00:38:08 Jacob Carlborg wrote:
> On 2011-02-03 07:21, Jonathan M Davis wrote:
> > On Wednesday 02 February 2011 21:26:00 Mandeep Singh Brar wrote:
> >> Hi,
> >> 
> >> Is there a method to access this reference of the container class
> >> from an inner class. i.e.
> >> class A {
> >> class B {
> >> 
> >>     methodM() {
> >>     
> >>        callAnotherM(A::this or A.this);
> >>     
> >>     }
> >> 
> >> }
> >> }
> > 
> > The outer class is referenced via the property outer. However, if the
> > inner class is static, then it has no such property and is not tied to a
> > specific instance of the outer class. In such a case, it can access the
> > private members of an instance of the outer class, but it's not tied to
> > an particular instance. Non-static inner classes (like yours above),
> > however, _are_ tied to a particular instance of the outer class, and
> > they have the outer property which is the this of the outer class.
> > 
> > - Jonathan M Davis
> 
> Just to add a note, you have to access the property via "this":
> 
> this.outer

That, I did not know. But I've never actually used an inner class in D thus far. 
I just read what TDPL says on it, and obviously I missed that point. Thanks.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list