clone method of Object
grauzone
none at example.net
Wed Apr 15 08:03:48 PDT 2009
Qian Xu wrote:
> grauzone wrote:
>> newobject.tupleof[i] = old.tupleof[i];
>
> If the current value of tupleof[i] is an object, the object will be
> referenced, won't it?
>
> Shall I write:
>
> auto elem = old.tupleof[i];
> static if (is(typeof(elem) == class))
> {
> newobject.tupleof[i] = clone(elem);
> }
> else
> {
> newobject.tupleof[i] = elem;
> }
Object graphs often contain circular references liker this:
class A {
B b;
}
class B {
A a;
}
auto a = new A();
auto b = new B();
a.b = b;
b.a = a;
Your recursive approach wouldn't quite work with that. Before cloning an
object, you'll first have to check if the object was already cloned. If
this is the case, use the previously created clone instead of making a
new clone.
>
>
>
> --Qian
More information about the Digitalmars-d-learn
mailing list