Unintentional sharing?
    Andy Valencia 
    dont at spam.me
       
    Thu Jun  6 17:49:39 UTC 2024
    
    
  
I was using instance initialization which allocated a new object. 
  My intention was this initialization would happen per-instance, 
but all instances appear to share the same sub-object?  That is, 
f1.b and f2.b appear to point to a single object?  Obviously I 
moved the new into the initializer code, but I hadn't appreciated 
how initial instance values were calculated once.  Interestingly, 
this makes it similar to how Python calculates default argument 
values for functions.
class Bar {
     int z = 3;
}
class Foo {
     auto b = new Bar();
}
void
main() {
     import std.stdio : writeln;
     auto f1 = new Foo(), f2 = new Foo();
     f1.b.z = 0;
     writeln(f2.b.z);
}
    
    
More information about the Digitalmars-d-learn
mailing list