R
richard d
I can basically see two ways of doing this, in both cases implementing
Clone() from the ICloneable interface:
Given:
class A : ICloneable { ... }
class B : A { ... }
1)
In (A)
public virtual object Clone() { return new A(this); }
protected A (A clonee) { /* copy from clonee, making further clones of
members where appropriate */ }
In (B)
public override object Clone() { return new B(this); }
protected B (B clonee) : base(clonee) { /* copy from clonee, making
furhter clones of members where appropriate */ }
This is the C++ way
2)
In (A)
public object Clone() { A clone = MemberwiseClone() as A;
clone.MakeDeep() ; return clone; }
protected virutal void MakeDeep() { /* replace shallow member copies
with deep copies */ }
In (B)
protected override void MakeDeep() { base.MakeDeep(); /* replace
shallow member copies with deep copies */ }
- What looks like the new c# way.
Unless MemberwiseClone is doing something behind the scenes, and this
might just be personal preference, I like method (1) better.
Is it just personal preference?
All the best
Richard
Clone() from the ICloneable interface:
Given:
class A : ICloneable { ... }
class B : A { ... }
1)
In (A)
public virtual object Clone() { return new A(this); }
protected A (A clonee) { /* copy from clonee, making further clones of
members where appropriate */ }
In (B)
public override object Clone() { return new B(this); }
protected B (B clonee) : base(clonee) { /* copy from clonee, making
furhter clones of members where appropriate */ }
This is the C++ way
2)
In (A)
public object Clone() { A clone = MemberwiseClone() as A;
clone.MakeDeep() ; return clone; }
protected virutal void MakeDeep() { /* replace shallow member copies
with deep copies */ }
In (B)
protected override void MakeDeep() { base.MakeDeep(); /* replace
shallow member copies with deep copies */ }
- What looks like the new c# way.
Unless MemberwiseClone is doing something behind the scenes, and this
might just be personal preference, I like method (1) better.
Is it just personal preference?
All the best
Richard