grauzone wrote:
Qian Xu wrote:Hi All,is there any (easy) way to clone an object or any other classes? --QianSimple answer: No. Complicated answer: Yes, but you have to write it yourself.Here's a nice starting point. You can use tupleof to get all members of a class. Note that this doesn't deal with superclasses, and members of superclasses are not copied:T clone(T)(T old) { auto newobject = new T(); foreach (int i, _; old.tupleof) { newobject.tupleof[i] = old.tupleof[i]; } return newobject; } cloned = clone(yourobject);
I should add that members of subclasses are not copied either. E.g. if you have
class A {int a;}
class B : A {int b;}
class C : B {int c;}
B someobject;
clone(someobject);
the clone method will only copy member b, but not a or c.
