Anyway to achieve the following
Rekel
paultjeadriaanse at gmail.com
Tue Aug 17 16:20:47 UTC 2021
On Saturday, 14 August 2021 at 20:50:47 UTC, Carl Sturtivant
wrote:
> ```
> struct S {
> int x = 1234;
> }
>
> void main() {
> import std.stdio;
> S s;
> //construction of a using &(s.x)
> auto a = Ref!(int)(&s.x);
> writeln(a); //displays 1234
> s.x += 1;
> writeln(a); //displays 1235
> a += 1;
> writeln(s.x); //displays 1236
> }
>
> struct Ref(T) {
> T* ptr;
> this(T* p) { ptr = p; }
> string toString() { import std.conv; return to!string(*ptr); }
> ref T var() { return *ptr; }
> alias var this;
> }
> ```
Wow you can alias this a function? TIL!
More information about the Digitalmars-d-learn
mailing list