Newbie: copy, assignment of class instances

bearophile bearophileHUGS at lycos.com
Thu May 20 13:08:04 PDT 2010


> Generally in D it's not a good idea to reassign the reference to a scoped class 

You can see it with this little D2 program:


import std.stdio: printf;
class Foo {
    int x;
    this(int xx) { this.x = xx; }
    ~this() { printf("Foo(%d) destructor\n", this.x); }
}
void main() {
    scope Foo f1 = new Foo(1);
    Foo f2 = new Foo(2);
    f1 = f2;
}

It prints just:
Foo(2) destructor

In general this is not good.
Bye,
bearophile


More information about the Digitalmars-d-learn mailing list