nested class inheritance

Michael Shulman viritrilbia at gmail.com
Tue May 31 16:57:52 PDT 2011


On Tue, May 31, 2011 at 2:24 PM, Simen Kjaeraas <simen.kjaras at gmail.com> wrote:
>> I have thought of a workaround with 'alias this':
>>
>> class Outer2 : Outer1 {
>>  class Inner2 {
>>    Inner1 _self;
>>    alias _self this;
>>    this() {
>>      _self = this.outer.new Inner1();
>>    }
>>  }
>> }
>>
>> This seems to work, but requires manually calling all the constructors
>> of Inner1 from corresponding constructors of Inner2.  Is there a better
>> way to do what I am after?
>
> Does your inner class require implicit access to the outer class? That is,
> could a static inner class work?

Thanks for the suggestion.  Yes, my inner class does require access to
the outer class.  I suppose it doesn't have to be *implicit* access;
the inner class could just keep an explicit reference to the outer
class.  (Is there ever a situation in which the implicit-ness of the
'outer' reference is necessary, rather than just convenient?)

I've also realized that my proposed workaround actually doesn't work,
because 'alias this' doesn't actually behave like subclassing with
respect to references.  That is, if Inner2 is 'alias this'ed to
Inner1, and I try to pass an Inner2 object to a function that's
expecting an Inner1, it actually just passes the _self Inner1 object
which knows nothing about Inner2 any more--right?

Mike


More information about the Digitalmars-d-learn mailing list