Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will objects be copied in assignment in D?

When I assign an object in D, will it be copied?

void main() {
    auto test = new Test(new Object());
    tset.obj;
}

class Test {
    public Object obj;

    public this(Object ref origObj) {
        obj = origObj; // Will this copy origObj into obj, or will origObj and obj point to the same data? (Is this a valid way to pass ownership without copying the object?)
    }
}
like image 897
Jeroen Avatar asked Feb 15 '23 16:02

Jeroen


1 Answers

Only the reference is copied, the object itself isn't duplicated. You can explicitly duplicate the object by using .dup though.

like image 131
kviiri Avatar answered Feb 24 '23 00:02

kviiri