On Thursday, 7 July 2016 at 09:46:25 UTC, Lodovico Giaretta wrote:
On Thursday, 7 July 2016 at 09:40:57 UTC, Lodovico Giaretta
wrote:
Initially it looks very surprising, but then if you add
`writeln(B.init.col[]);` you can easily find out what's going
on.
And I'm quite sure it's expected behaviour.
An RBTree is just a pointer to the memory containing the actual
tree. Your `col`s have different addresses because they are
different copies of the same pointer. If you cast `col` to a
pointer and write the address it's pointing at, you find out
that the two structures are pointing to the same memory.
This is because assignments used in a structure declaration are
not re-executed for each instantiation. Instead, they are
executed one to create the `.init` member of the type, which is
then bit-copied on every other instance. So your code does this:
B.init.col = new RBTree(...);
B b1 = B.init;
B b2 = B.init;
Still, if I make B a class instead of a struct (and instantiate
using new), I get the same result... Do classes behave the same
as structs in this regard? I mean, static initializers compared
to "this" (now that I write it, I guess the word "static" gives
an important clue...).
Anyway, glad to know what was happening... still a bit
unintuitive, but I guess it makes sense after all.
Thanks!!