nested class inheritance

Era Scarecrow rtcvb32 at yahoo.com
Fri Jul 13 11:25:56 PDT 2012


On Friday, 13 July 2012 at 18:00:05 UTC, Gor Gyolchanyan wrote:
> Doesn't this make sense?
>
> class Fruit
> {
>     class Seed { }
> }
>
> class Apple: Fruit
> {
>      class AppleSeed: Fruit.Seed { }
> }
>
> This means, that as Fruit.Seed needs access to enclosing Fruit, 
> AppleSeed, being a Fruit.Seed will also have access to 
> enclosing Fruit, which is Apple.
> DMD 0.259 has this to say:
>
> Error: class main.Apple.AppleSeed is nested within Apple, but  
> super class Seed is nested within Fruit
>
> Which doesn't make sense, IMO. What do you guys think about 
> this? Who makes more sense me or DMD 2.059?

  I'd say DMD makes more sense. Seed has knowledge of itself AND 
Fruit. So if it's following the same rules and for how it makes 
sense, then unless you add static to Seed, then you won't get it 
to work. Reminds me of when I used Java years ago experimenting 
with inner classes, came to a distinct conclusion of object 
referencing and why it didn't want to work without an outer 
object.

  Consider: fails on seed, not seed2.

class Fruit {
  int x;
  class Seed {
   void oneMoreToX() {
    x++; //knows about Fruit.x, even if not instantiated
   }
  }

  static class Seed2 {
   void oneMoreToX() {
//  x++; //fails to compile, no knowledge of Fruit
   }
  }
}

class Apple: Fruit {
  class AppleSeed: Fruit.Seed { } //fails (no outer object 
(Fruit.x) and makes no sense)
  class AppleSeed2: Fruit.Seed2 { } //works fine
}


More information about the Digitalmars-d mailing list