www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Getting 'this' of outer class?

reply Nick <Nick_member pathlink.com> writes:
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
Feb 19 2006
next sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"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).
Feb 19 2006
prev sibling parent reply David Medlock <noone nowhere.com> writes:
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
 
 

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
Feb 20 2006
parent David Mac <davidjohnmac gmail.com> writes:
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;}
                 //=======//

  }
}
Sep 16 2006