How to access private variable of outer class from an inner struct
thebluepandabear
therealbluepandabear at protonmail.com
Sun Jan 15 23:05:08 UTC 2023
On Sunday, 15 January 2023 at 13:23:20 UTC, matheus wrote:
> On Sunday, 15 January 2023 at 12:44:51 UTC, thebluepandabear
> wrote:
>> ...
>> How will the variable `outer` become the reference to the
>> current `X` object (if that makes sense?). Does the compiler
>> do it automatically?
>
> I think you'll need to do this:
>
> class X {
> private int num;
>
> struct Y {
> X outer;
> int fun() { return outer.num; }
> }
>
> Y y;
> this(){
> y = Y(this);
> }
> }
>
> void main(){
> import std.stdio : writeln;
> auto x = new X();
> x.num = 10;
> writeln(x.num);
> writeln(x.y.fun());
> }
>
> Prints:
>
> 10
> 10
>
> Matheus.
ah, that's annoying, but I guess it's the only solution as it
stands.
More information about the Digitalmars-d-learn
mailing list