inner member classes in final outer class

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Sat Sep 15 15:09:32 PDT 2007


coxalan wrote:
> Consider this code:
> 
> class Outer {
>     class Inner {
>     }
> }
> 
> void main() {
>     final Outer o = new Outer;
>     o.Inner i1 = o.new Inner;
>     o.Inner i2 = o.new Inner;
>     o.Inner i3 = o.new Inner;
> }
> 
> According to http://www.digitalmars.com/d/class.html, each "o.Inner" instance contains a "context pointer" to the enclosing outer class.
> 
> But in this case the enclosing outer class "o" is final, so in my opinion there is no need to store the context pointer in i1, i2 and i3 again and again. Instead, always the address stored in the reference "o" could be used.

I'm pretty sure the context pointer is already a copy of 'o' (not a 
pointer to it as you seem to think).

I just looked it up; from the documentation (the page you linked above):
=====
Non-static nested classes work by containing an extra hidden member 
(called the context pointer) that is the frame pointer of the enclosing 
function if it is nested inside a function, or the this of the enclosing 
class's instance if it is nested inside a class.
=====

In the code above the class is nested in another class (not a function), 
so the context pointer is the 'this' of the enclosing class, i.e. a copy 
of 'o'.



More information about the Digitalmars-d mailing list