Array of class intances

YY yyudhistira at hotmail.com
Tue Apr 8 07:13:36 PDT 2008


Suppose I have a class :

class C {
  int a;
  int b;
  this(int a1, int b1) {
    a = a1;
    b = b1;
  }
}

And I want to make an array of class C instances :

C[] inst;
inst ~= new C(1,2);
inst ~= new C(3,4);

What happened if I make a copy of inst using dup? Are the values or the pointers are copied?

C[] copyinst = inst.dup;

When I tried to assert(inst.ptr == copyinst.ptr) it fails, which means it copies the contents.

But when I do this :

inst.a += 3;
assert(inst.a != copyinst.a);

It also fails.

What's the best method to copy (clone) array of instances?



More information about the Digitalmars-d-learn mailing list