Confused about Inner Classes

Janice Caron caron800 at googlemail.com
Mon Oct 22 04:38:22 PDT 2007


On 10/22/07, Bruce Adams <tortoise_74 at yeah.who.co.uk> wrote:
>    Just to check something here. I may be having a stupid day here. Are you saying there is a pointer member available from any inner class to the outer?

I believe that is the case, yes.

>. But it also looks like the outer class always has at least one
instance of the inner class.

Other way round, I think. The inner class has always has exactly one
instance of the outer class (accessible via the member "outer", or
implicitly). The outer class, by contrast, always has exactly one
/declaration/ of the inner class.

It's definitely weird. I tried this earlier on, and it compiled. (Hope
I'm remembering this right!)

abstract class OuterA
{
    class Inner
    {
    }
}

class OuterB : OuterA
{
    Inner getInner()
    {
        return new Inner());
    }
}

auto a = (new OuterB).getInner;

This /looks/ totally weird. The class OuterB has no inner class, and
yet it is still able to do new Inner - which is an inner class of
OuterA. But it's allowed because OuterA is the superclass of OuterB,
so it looks like classes inherit the inner class declarations of
parent classes. Conversely, instances of Inner would have access to
OuterB's variables (and therefore, OuterA's variables).

And no - I haven't really got my head round it either. I'm just
playing around, seeing what compiles and what doesn't and trying to
grok it.


> This suggests that a D best coding practice is to make helper types a member of the same module but never nest them. Otherwise, how do I declare something other than a 1-to-1 relationship with a nested class?

My suspicion is you haven't understood it. Of course, I say this in
the full knowledge that I haven't understood it either, and because it
seems very hard to understand.



More information about the Digitalmars-d mailing list