Angry citizen stikes once again. forward references problems

Jarrett Billingsley kb3ctd2 at yahoo.com
Sun Jul 30 15:57:14 PDT 2006


"Derek" <derek at psyc.ward> wrote in message 
news:zbzgquaqpewl$.brpiyqr0sclz.dlg at 40tude.net...
> I don't understand this Dawid. It seems you are trying to use a nested
> class inside another class, and I'm sure you can't and shouldn't do that.

Why not?  What about a parent "factory" class, which needs to create 
instances of a nested class, which the user can use?  The nested class can 
still access its outer class, and the user can use the nested class 
instances just fine.

> I thought the purpose of nested classes was to restrict the scope of the
> nested class to its parent and thus trying to use it outside of the parent
> doesn't seem right to me.

Sometimes that's true.  Sometimes you just want the nested class to have 
access to the outer class.  Granted, you can do this with friend classes, 
but.. as mentioned above, if you want an "owner/owned" relationship between 
the classes, using a nested class seems somewhat more elegant.

> D won't let you do even the more simple case ...
>
> //------- test3.d -----
> class X
> {
>  static class NX
>  {
>  }
> }
>
> class Y
> {
>     NX q; // test3.d(10): identifier 'NX' is not defined
> }
> // -------------------- //
>
> I think dmd is working correctly, though the error messages are not 
> exactly
> helpful.

Namespaces, namespaces.

Class Y
{
    X.NX q;
}

And you could even "alias X.NX NX;" if you wanted to.

In addition, you'll notice that Walter even added syntax in a recent update 
to allow you to create instances of nested classes using an outer class 
reference:

X x = new X;
X.NX nx = x.new NX;

Which seems like you're almost.. well maybe not _encouraged_ but certainly 
not _blocked from_ creating instances of nested classes outside the owner 
class. 





More information about the Digitalmars-d mailing list