On Thu, 22 Oct 2009 18:07:15 +0400, Andrei Alexandrescu <[email protected]> wrote:

Refer to:

http://www.digitalmars.com/d/2.0/struct.html

and scroll down to the last section, "Nested Structs". A struct defined inside a function has a hidden pointer to that function's stack frame and therefore can use function's local variables.

Nested classes do a similar trick, but for those there's a bit of motivation - you could create a nested class and use it as a sort of closure by returning a base class or interface of it.

With nested structs, however, you can't do much. You can pass them to a template, but I can't see some solid use cases there. My understanding is that nested structs have been implemented for completeness and consistency with nested classes.

Any good example of nested struct uses?


Andrei

Everything you can do with nested struct is doable with a nested class as well (marking methods as final, using scope to allocate instance on stack etc). I don't think there's anything specific to nested struct. One thing I can think of is that you are using some template that works with structs only, but then you may create a nested class and nest a static struct inside it (not that I would recommend doing so but still) :)

Reply via email to