Thread overview
Getting 'this' of outer class?
Feb 19, 2006
Nick
Feb 20, 2006
David Medlock
Sep 16, 2006
David Mac
February 19, 2006
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
"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
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
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;}
                 //=======//

  }
}