www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Accessing this of containing class

reply Mandeep Singh Brar <mandeep brars.co.in> writes:
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);
   }
}
}

Thanks
Mandeep
Feb 02 2011
next sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
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
Feb 02 2011
parent reply Jacob Carlborg <doob me.com> writes:
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 -- /Jacob Carlborg
Feb 03 2011
parent Ary Manzana <ary esperanto.org.ar> writes:
On 2/3/11 10:29 AM, Steven Schveighoffer wrote:
 On Thu, 03 Feb 2011 03:43:43 -0500, Jonathan M Davis
 <jmdavisProg gmx.com> wrote:

 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

 specific instance of the outer class. In such a case, it can access

 private members of an instance of the outer class, but it's not

 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.

I have noticed that, is that a bug? I always thought it strange, since outer is a keyword, that you need to do this.outer. -Steve

I don't think outer is a keyword.
Feb 03 2011
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
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
Feb 03 2011
prev sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Thu, 03 Feb 2011 03:43:43 -0500, Jonathan M Davis <jmdavisProg gmx.com>  
wrote:

 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

 specific instance of the outer class. In such a case, it can access  

 private members of an instance of the outer class, but it's not tied  

 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.

I have noticed that, is that a bug? I always thought it strange, since outer is a keyword, that you need to do this.outer. -Steve
Feb 03 2011
next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Thursday, February 03, 2011 10:36:29 Ary Manzana wrote:
 On 2/3/11 10:29 AM, Steven Schveighoffer wrote:
 On Thu, 03 Feb 2011 03:43:43 -0500, Jonathan M Davis
 
 <jmdavisProg gmx.com> wrote:
 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.

I have noticed that, is that a bug? I always thought it strange, since outer is a keyword, that you need to do this.outer. -Steve

I don't think outer is a keyword.

It's not a keyword. It's a property of non-static inner classes. It's like length on an array. It's always there, but it's not a keyword. - Jonathan M Davis
Feb 03 2011
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Thu, 03 Feb 2011 13:53:22 -0500, Jonathan M Davis <jmdavisProg gmx.com>  
wrote:

 On Thursday, February 03, 2011 10:36:29 Ary Manzana wrote:
 On 2/3/11 10:29 AM, Steven Schveighoffer wrote:
 I have noticed that, is that a bug? I always thought it strange, since
 outer is a keyword, that you need to do this.outer.

 -Steve

I don't think outer is a keyword.

It's not a keyword. It's a property of non-static inner classes. It's like length on an array. It's always there, but it's not a keyword.

You're right, it isn't. My text editor highlights it like it is, so I just assumed... My question still stands though, why the requirement to use this.outer instead of just outer? -Steve
Feb 03 2011