How to access private variable of outer class from an inner struct
Paul Backus
snarwin at gmail.com
Sun Jan 15 12:37:43 UTC 2023
On Sunday, 15 January 2023 at 12:26:15 UTC, thebluepandabear
wrote:
> If I have the following code:
>
> ```D
> class X {
> private int num;
>
> struct Y {
> // how to access num?
> }
> }
> ```
>
> How would I access `num` from `Y`?
>
> Whenever I try to I get a compilation error.
>
> I believe that it's possible for nested/inner classes, but I
> don't know if it's possible for structs.
>
> Help would be apprciated.
I don't think this works for structs. As a workaround, you give
your struct an explicit reference to the outer class, like this:
```D
class X {
private int num;
struct Y {
X outer;
int fun() { return outer.num; }
}
}
```
More information about the Digitalmars-d-learn
mailing list