Super easy struct construction question that I'm embarrassed to ask.
WhatMeWorry
kheaser at gmail.com
Thu Jan 9 22:01:59 UTC 2025
```
import std.stdio;
struct Location {
int r;
int c;
}
struct Node {
this(Location locaction, uint f) {
this.location = location;
this.f = f;
}
Location location;
uint f;
}
void main() {
Node n = Node(Location(1,2), 33);
writeln("n = ", n);
}
---------------------------------------------------
produces:
n = Node(Location(0, 0), 33)
when I expected
n = Node(Location(1, 2), 33)
So structs are value objects (not reference like classes). So it
is passed by value. So a copy of 1 and 2 is made upon entering
the constructor this. But aren't these copies then placed into
the Node structure n (for the lifetime of n). So that writeln
should produce Location(1, 2), 33.
More information about the Digitalmars-d-learn
mailing list