Inner classes - More expressiveness needed

Steven Schveighoffer schveiguy at yahoo.com
Thu Oct 25 08:18:05 PDT 2007


"Janice Caron" wrote
>I can't call this a bug, because I think it's behaving as documented.
> Nonetheless, it seems to be that D could be more expressive. The
> following won't compile:
>
>    class Outer
>    {
>        int n;
>
>        class Inner
>        {
>            static int f() { return n; }
>        }
>    }
>
> The error is:
> Error: need 'this' to access member n
>
> The problem is that the word "static" is horribly overused, and
> doubles up to mean BOTH "the function does not have a this pointer"
> AND "this function does not have an outer pointer".
>
> There seems no way to express "please can my function have an outer
> pointer but not a this pointer" (which is what I was trying to
> achieve).

And what would you have the outer member point to? :)  It needs to point to 
an instance of the outer class, and if you have a static function, there is 
no instance of an inner or outer class.

If you need a function that requires an instance of the outer class, but not 
an instance of the inner class, the function belongs in the outer class.  I 
think that is what you wanted?

> There also seems no way to express "please can my function have a this
> pointer but not an outer pointer". (I wasn't trying to achieve that,
> but it could conceivably be useful).

This is done by creating another class in the same module.

-Steve 





More information about the Digitalmars-d mailing list