Connecting Tech Pros Worldwide Forums | Help | Site Map

Cloning

Tom
Guest
 
Posts: n/a
#1: Nov 17 '05
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..



billr
Guest
 
Posts: n/a
#2: Nov 17 '05

re: Cloning


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:
[color=blue]
> 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..
>
>
>[/color]
billr
Guest
 
Posts: n/a
#3: Nov 17 '05

re: Cloning


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:
[color=blue]
> 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..
>
>
>[/color]
Tom
Guest
 
Posts: n/a
#4: Nov 17 '05

re: Cloning


Thanks, it's a great and easy idea. The copy contructor will be very similar
to my present object contructor!,

Tom
[color=blue]
> 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[/color]


Tom
Guest
 
Posts: n/a
#5: Nov 17 '05

re: Cloning


Thanks, it's a great and easy idea. The copy contructor will be very similar
to my present object contructor!,

Tom
[color=blue]
> 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[/color]


Ignacio Machin \( .NET/ C# MVP \)
Guest
 
Posts: n/a
#6: Nov 17 '05

re: Cloning


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" <tomasz.smykowski@absysco.pl> wrote in message
news:deevpa$fef$1@nemesis.news.tpi.pl...[color=blue]
> 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..
>[/color]


Ignacio Machin \( .NET/ C# MVP \)
Guest
 
Posts: n/a
#7: Nov 17 '05

re: Cloning


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" <tomasz.smykowski@absysco.pl> wrote in message
news:deevpa$fef$1@nemesis.news.tpi.pl...[color=blue]
> 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..
>[/color]


Michael S
Guest
 
Posts: n/a
#8: Nov 17 '05

re: Cloning


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" <tomasz.smykowski@absysco.pl> wrote in message
news:deevpa$fef$1@nemesis.news.tpi.pl...[color=blue]
> 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..
>[/color]


Michael S
Guest
 
Posts: n/a
#9: Nov 17 '05

re: Cloning


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" <tomasz.smykowski@absysco.pl> wrote in message
news:deevpa$fef$1@nemesis.news.tpi.pl...[color=blue]
> 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..
>[/color]


Closed Thread