469,275 Members | 1,424 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

Cloning

Tom
I've a problem. I want to clone an object having a list of other objects
(and so on :/). Do you know any other way than ICloneable.Clone()
implementation for all classes in the way? Help..
Nov 17 '05 #1
8 4637
object.Clone() is generally used for a shallow copy

provide a copy constructor for a deep copy

MyClass(MyClass obj) { /* ... */ }

obviously, you don't have to follow the above advice, I'm saying you HAVE to
do that ;o)

--
Of all words of tongue and pen, the saddest are: "It might have been"

Bill.Richards @ greyskin .co .uk
http://greyskin.co.uk
"Tom" wrote:
I've a problem. I want to clone an object having a list of other objects
(and so on :/). Do you know any other way than ICloneable.Clone()
implementation for all classes in the way? Help..

Nov 17 '05 #2
object.Clone() is generally used for a shallow copy

provide a copy constructor for a deep copy

MyClass(MyClass obj) { /* ... */ }

obviously, you don't have to follow the above advice, I'm saying you HAVE to
do that ;o)

--
Of all words of tongue and pen, the saddest are: "It might have been"

Bill.Richards @ greyskin .co .uk
http://greyskin.co.uk
"Tom" wrote:
I've a problem. I want to clone an object having a list of other objects
(and so on :/). Do you know any other way than ICloneable.Clone()
implementation for all classes in the way? Help..

Nov 17 '05 #3
Tom
Thanks, it's a great and easy idea. The copy contructor will be very similar
to my present object contructor!,

Tom
object.Clone() is generally used for a shallow copy

provide a copy constructor for a deep copy

MyClass(MyClass obj) { /* ... */ }

obviously, you don't have to follow the above advice, I'm saying you HAVE
to
do that ;o)

--
Of all words of tongue and pen, the saddest are: "It might have been"

Bill.Richards @ greyskin .co .uk
http://greyskin.co.uk

Nov 17 '05 #4
Tom
Thanks, it's a great and easy idea. The copy contructor will be very similar
to my present object contructor!,

Tom
object.Clone() is generally used for a shallow copy

provide a copy constructor for a deep copy

MyClass(MyClass obj) { /* ... */ }

obviously, you don't have to follow the above advice, I'm saying you HAVE
to
do that ;o)

--
Of all words of tongue and pen, the saddest are: "It might have been"

Bill.Richards @ greyskin .co .uk
http://greyskin.co.uk

Nov 17 '05 #5
Hi,

ICloneable is a clear indication than your object provide this feature, if
not you can use any method you want, like a public method :
public MyList CreateNewList(){}

also remember that you only copied the list, not the objects themselves. if
you change a property of one of the listed elements this change will be
accesible from the other list.
if you want to do a deep copy the listed object should provide the same
functionality.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Tom" <to**************@absysco.pl> wrote in message
news:de**********@nemesis.news.tpi.pl...
I've a problem. I want to clone an object having a list of other objects
(and so on :/). Do you know any other way than ICloneable.Clone()
implementation for all classes in the way? Help..

Nov 17 '05 #6
Hi,

ICloneable is a clear indication than your object provide this feature, if
not you can use any method you want, like a public method :
public MyList CreateNewList(){}

also remember that you only copied the list, not the objects themselves. if
you change a property of one of the listed elements this change will be
accesible from the other list.
if you want to do a deep copy the listed object should provide the same
functionality.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Tom" <to**************@absysco.pl> wrote in message
news:de**********@nemesis.news.tpi.pl...
I've a problem. I want to clone an object having a list of other objects
(and so on :/). Do you know any other way than ICloneable.Clone()
implementation for all classes in the way? Help..

Nov 17 '05 #7
Hi.

On this subject I would recommend you to never implement a
ICloneable.Clone() in .NET 1.1.
This method always returns Object and needs to be downcasted. Too general
and way too ugly in my view.

It is much better to write your own Clone without the interface but with the
correct type. But I still think this should be avoided as this could f'***
up future subclasses.

Another way to go is by a cloning constructor as billr pointed out. This to
me signals an exact identical copy.
Or you can go the Delphi way and create a method called Assign(MyClass obj)
that do a partial cloning by sucking the values needed from an existing
MyClass object.

In NET 2.0 we get generics and will be able to use the
ICloneable<T>.Clone() which is awesome and will be our prefered way of
cloning stuff.

Finally, I would like to say that cloning should always be shallow. If you
need a deep copy, then name the method Copy or Dulplicate or something
similar.

I think there should be a ICopyable<T>.Copy in .NET =)

Happy Cloning
- Michael S
"Tom" <to**************@absysco.pl> wrote in message
news:de**********@nemesis.news.tpi.pl...
I've a problem. I want to clone an object having a list of other objects
(and so on :/). Do you know any other way than ICloneable.Clone()
implementation for all classes in the way? Help..

Nov 17 '05 #8
Hi.

On this subject I would recommend you to never implement a
ICloneable.Clone() in .NET 1.1.
This method always returns Object and needs to be downcasted. Too general
and way too ugly in my view.

It is much better to write your own Clone without the interface but with the
correct type. But I still think this should be avoided as this could f'***
up future subclasses.

Another way to go is by a cloning constructor as billr pointed out. This to
me signals an exact identical copy.
Or you can go the Delphi way and create a method called Assign(MyClass obj)
that do a partial cloning by sucking the values needed from an existing
MyClass object.

In NET 2.0 we get generics and will be able to use the
ICloneable<T>.Clone() which is awesome and will be our prefered way of
cloning stuff.

Finally, I would like to say that cloning should always be shallow. If you
need a deep copy, then name the method Copy or Dulplicate or something
similar.

I think there should be a ICopyable<T>.Copy in .NET =)

Happy Cloning
- Michael S
"Tom" <to**************@absysco.pl> wrote in message
news:de**********@nemesis.news.tpi.pl...
I've a problem. I want to clone an object having a list of other objects
(and so on :/). Do you know any other way than ICloneable.Clone()
implementation for all classes in the way? Help..

Nov 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Tom | last post: by
2 posts views Thread by Hendrik Schober | last post: by
3 posts views Thread by AVL | last post: by
6 posts views Thread by J Williams | last post: by
10 posts views Thread by pazabo | last post: by
1 post views Thread by Dan Dorey | last post: by
reply views Thread by Chris | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.