digitalmars.D.learn - How to access private variable of outer class from an inner struct
- thebluepandabear (14/14) Jan 15 2023 If I have the following code:
- Paul Backus (13/27) Jan 15 2023 I don't think this works for structs. As a workaround, you give
- thebluepandabear (5/38) Jan 15 2023 Thanks.
- Hipreme (4/8) Jan 15 2023 You'll have to create your struct like `return Y(this)`. It
- matheus (25/29) Jan 15 2023 I think you'll need to do this:
- thebluepandabear (3/32) Jan 15 2023 ah, that's annoying, but I guess it's the only solution as it
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.
Jan 15 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; } } } ```
Jan 15 2023
On Sunday, 15 January 2023 at 12:37:43 UTC, Paul Backus wrote:On Sunday, 15 January 2023 at 12:26:15 UTC, thebluepandabear wrote:Thanks. How will the variable `outer` become the reference to the current `X` object (if that makes sense?). Does the compiler do it automatically?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; } } } ```
Jan 15 2023
On Sunday, 15 January 2023 at 12:44:51 UTC, thebluepandabear wrote:Thanks. How will the variable `outer` become the reference to the current `X` object (if that makes sense?). Does the compiler do it automatically?You'll have to create your struct like `return Y(this)`. It basically depends on how you're using the struct
Jan 15 2023
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.
Jan 15 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:ah, that's annoying, but I guess it's the only solution as it stands.... 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.
Jan 15 2023