473,396 Members | 1,590 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Cloning Question

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;

/* I'm resetting some of the members (to be safe) since the cloned
copy will have to be reinitialized before being used. */

ret.m_initialized = false;
ret.m_deviceID = null;
ret.m_currentNode = null;
ret.m_asyncCommandProcessor = null;
ret.m_syncCommandProcessor = null;

if (this.m_rootNode != null)
{
ret.m_rootNode = this.m_rootNode.Clone() as INode;
}

The NON working method:

/* I thought it would be simpler to just new up a new class in this
case as I don't really want to copy most of the members */

CallflowBase ret = new CallflowBase();
ret.m_callflowName = this.m_callflowName;

if (this.m_rootNode != null)
{
ret.m_rootNode = this.m_rootNode.Clone() as INode;
}

But it appears that this method of cloning fails since I end up
pointing to the same object. Am I missing something obvious here?

Thanks,
Dan

Sep 21 '07 #1
1 1765
Dan Dorey wrote:
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.
You haven't posted complete code. For best results, you need to post a
concise-but-complete example of code that demonstrates the problem. In
this case, that would mean a minimal class posted with two complete
methods to clone, one for each technique you're using.

For what it's worth, it's my opinion that the code you posted isn't
really cloning anyway, so you shouldn't call it cloning. I suppose
there's some wiggle-room semantically, but so little of the original
object remains that I think it's very misleading in the code to use the
term "clone" to describe the operation.

And finally, some specific comments (none of which suggest what the
problem is, as far I know):
The working method:

CallflowBase ret = null;
ret = this.MemberwiseClone() as CallflowBase;
There's no need to initialize the variable to null if you're just going
to set it immediately after.

Otherwise, this seems fine and it should return a new instance of your
object, assuming you don't change "ret" later and return the value in "ret".
[...]
/* I thought it would be simpler to just new up a new class in this
case as I don't really want to copy most of the members */
I think so too. And because you don't want to copy most of the members,
I wouldn't actually call it a "clone". :)
CallflowBase ret = new CallflowBase();
This seems fine and should return a new instance of your object (with
the same caveats noted above).
[...]
But it appears that this method of cloning fails since I end up
pointing to the same object. Am I missing something obvious here?
If so, you didn't post the code that is causing the problems. At least,
not as near as I can tell.

In both versions, you initialize a variable "ret" to be a new instance
of your object. So, it seems that the problem is not in the code you
posted. Some suggestions as to what _might_ be the problem:

* You are not actually returning that value held by the "ret" local
variable. What value you are returning, I can't say, since you didn't
post a complete example of the code.

* You are incorrectly determining that you "end up pointing to the
same object". Do you actually have a reference to the same object? How
exactly did you make that determination? When you clone an object,
obviously the resulting object can in a lot of ways look a lot like the
original. :)

There are other ways you may have made a mistake, but it's not really
possible with the code you posted to say for sure, I think.

Pete
Sep 21 '07 #2

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

Similar topics

4
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...
7
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...
0
by: meh | last post by:
Greetings all; Got some questions about cloning a treenode.... In this example: private void button4_Click(object sender, System.EventArgs e) { TreeNode lastNode = treeView1.Nodes....
8
by: Tom | last post by:
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..
2
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
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...
6
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...
3
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...
0
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...
0
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,...

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.