Array of class intances

BCS BCS at pathlink.com
Tue Apr 8 09:22:05 PDT 2008


Regan Heath wrote:

> C[] copyinst;
> foreach(i; inst)
>     copyinst ~= i.dup;
> 
> Regan

this will give somewhat better performance because the ~= will allocate 
and copy a lot.

C[] copyinst;
copyinst.length = inst.length;
foreach(i,c; inst) copyinst[i] = c.dup;


if you want compact code and don't mind it being a bit confusing:

C[] copyinst = inst.dup;
foreach(inout i; inst) i = i.dup;


More information about the Digitalmars-d-learn mailing list