Nested Structs
Steven Schveighoffer
schveiguy at yahoo.com
Mon Dec 27 08:51:00 PST 2010
On Mon, 27 Dec 2010 08:54:37 -0500, d coder <dlang.coder at gmail.com> wrote:
> Greetings All
>
> I have a situation where I have a struct nested inside a class. I
> would like to make the enclosing class' members visible inside the
> nested struct's constructor. I see that such preposition is feasible
> for nested classes, but not for nested structs. Why so?
A struct nested in a class does not have a hidden "outer" pointer as a
nested class does. It's because a struct is generally more bare-bones
than a class (which has loads of hidden pieces: vtable, interfaces,
classinfo, etc.). Also, instantiating such a struct does not tie it to a
class instance.
>
> Are there some alternative constructs which could give me this
> behavior for nested constructs?
You need to implement this behavior on your own:
class C
{
struct S
{
private C _outer;
this(C c) { this._outer = c; _outer.x = 5;} // access enclosing class
instance via _outer.
}
int x;
}
-Steve
More information about the Digitalmars-d-learn
mailing list