08.09.2016 15:24, lobo пишет:
I am confused, which is normal, but I'd appreciate some help :-)
If I create N classes in a for loop they are all the same instance. I
would expect each to be a unique instance of the class. See the code below
---
class C {}
void main() {
import std.stdio;
auto c1 = new C();
writefln("c1:%s", &c1); // OK, instance c1 is unique
auto c2 = new C(); // OK, instance c2 is unqiue
writefln("c2:%s", &c2);
foreach(a; 0..10) {
C c = new C(); // All instances are the same object with the
same address?
writefln("c:%s", &c);
}
}
---
This isn't what I expected. What could I be doing wrong?
Thanks,
lobo
&c is address of the variable c, that is allocated on the stack and has
the same address on every iteration
cast(void*)c return the value of variable c that is address of a class
instance and is different for every iteration
in other words
&c is address of pointer
cast(void*)c is the pointer itself