inner member classes in final outer class

coxalan coxalan at web.de
Sat Sep 15 14:44:55 PDT 2007


Hello,

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.

Now two questions:

1) Would it be possible to have an optimization in the D compiler which removes the context pointer in the case of final outer classes? Or will that result in other problems I currently do not realize?

2) With the current dmd compiler, is there a way (maybe using template magic) for a similar object design which circumvents the context pointer?

A suggestion was made here:
http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=58154
But the problem with that code is that I cannot have more than one outer class of the same type possessing inner classes.

Thanks,

coxalan



More information about the Digitalmars-d mailing list