Never called destructor
Zarathustra
adam.chrapkowski at gmail.com
Thu Mar 25 14:36:53 PDT 2010
Why, in the following piece of code, destructor for 'x' is never called?
class A{ char [] s; this(char [] o_s){ s = o_s; } ~this(){ writefln(s); }; }
void main(){
A y;
{
scope A x = new A("x".dup); y = new A("y".dup);
x = y; //
}
}
output:
y // where is x
Destructors for scope objects are executed, because:
void main(){
A y;
{
scope A x = new A("x".dup); y = new A("y".dup);
// x = y;
}
}
output:
x
y
and for assignment too:
void main(){
A y;
{
A x = new A("x".dup); y = new A("y".dup);
x = y;
}
}
output:
y
x
More information about the Digitalmars-d-learn
mailing list