nested class inheritance

Michael Shulman viritrilbia at gmail.com
Tue May 31 11:17:23 PDT 2011


Hi,

I have a class which defines a nested class:

class Outer1 {
  class Inner1 { }
}

Now I want to inherit from Outer1, to modify its behavior in a way
which also involves modifying the behavior of the corresponding inner
objects.  My first instinct was to write

class Outer2 : Outer1 {
  class Inner2 : Inner1 { }
}

but the compiler does not allow this.  I guess that a nested class can
only be subclassed by a class nested in the same outer class.
Obviously it doesn't make sense to subclass a nested class in
arbitrary other places, but when nested in a subclass of the outer
class it seems sensible to me.

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?

Thanks!
Mike


More information about the Digitalmars-d-learn mailing list