473,554 Members | 2,911 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Clon e()
implementation for all classes in the way? Help..
Nov 17 '05 #1
8 4859
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.Clon e()
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.Clon e()
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.Clon e()
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.Clon e()
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.Clon e() 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>.C lone() 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>.Co py 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.Clon e()
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.Clon e() 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>.C lone() 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>.Co py 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.Clon e()
implementation for all classes in the way? Help..

Nov 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2450
by: Tom | last post by:
Hey ho, The release of PHP5 seemed like a good reason to try to learn it once more... I'm reading through the O'Reilly PHP/MySQL book and right now, I'm fiddling with objects. I just discovered the proper way to clone objects is *not* "$b = $a->__clone();", as it reads in the book, but "$b = clone $a". Right?
7
2708
by: sonic | last post by:
Hello, I am cloning a table row which contains images that have behaviors attached to them as well as onclick events. The problem is that the cloned row seems to be executing the behavior/events on the original rows image. I overwrote the onclick as well as attached a behavior to the image in cloned row, but events such as onmouseover...
2
1751
by: Hendrik Schober | last post by:
Hi, I need something like this: class X { private: struct Impl_ { virtual ~Impl_() {} virtual Impl_* clone() const = 0; };
3
1715
by: AVL | last post by:
Hi, I've a query in cloning. How cloning is different from creating a new instance of an object.? I suppose cloning also creates a new object and copies the exisitng object's data. Where and when should use cloning????//
6
8383
by: J Williams | last post by:
I'm using axWebBrowser control and HTML DOM in a VB .NET Windows application to create a new HTML document by cloning nodes. The function below is called from the axWebBrowser1_DocumentComplete event using: Dim mNewDoc As mshtml.IHTMLDocument3 mNewDoc = NewDoc(axWebBrowser1.Document) Private Function NewDoc(ByVal mInputDoc As...
10
7594
by: pazabo | last post by:
Hi, I'm trying to somehow implement polymorphic object cloning (just as it is in Java), but when I write: class Object { public: virtual Object * clone() const = 0; // ... }
3
8741
by: raylopez99 | last post by:
The "C# Cookbook" (O'Reilly / Jay Hilyard), section 3.26, is on deep cloning versus shallow cloning. The scanned pages of this book are found here: http://www.sendspace.com/file/mjyocg (Word format, 3 pp) My question, coming from a C++ background where deep copying is done, is why in C# you would do either deep or shallow copying as...
1
1774
by: Dan Dorey | last post by:
I've implemented the ICloneable interface on one of my class. I've written this simple code in two different ways and I think both should work but it's not the case and I'm curious to understand why. The working method: CallflowBase ret = null; ret = this.MemberwiseClone() as CallflowBase; ret.m_callflowName = this.m_callflowName;
0
1161
by: Chris | last post by:
Hi All. I'm cloning a treenode, like so: TreeNode trNewTemp = new TreeNode(); trNewTemp = ((((ICloneable)trTempNode).Clone()) as TreeNode); Now the cloning works- trNewTemp has the same content and properties as trTempNode. The problem is, the treenode I'm cloning has multiple childnodes, and childnodes of those childnodes.... it's...
0
7581
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7782
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8018
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7541
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7872
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6123
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5142
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3545
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
823
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.