473,327 Members | 2,103 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,327 software developers and data experts.

what's the difference btn Clone an object and assign the reference

Hey, I have a problem to understand the underlaying of
clone method. Hope someone can help me out.

Let's say,
clonedObject = originalObject.clone()

(syntax may be wrong, but you know what I mean, I hope )

Why can't I just do this, instead?

clonedObject = originalObject

Any comment is more than welcome.
Thanks in advance.

Vincent
Jul 21 '05 #1
4 2244
Think about it, cloning is making a copy of something. Suppose that you
have the following code.

object a = new MyObject();
object b = a

If you make changes to object "a", that same change will be reflected in "b"
simply because they both share the same memory location. They are the same
object.

Now, if I do

object a = new MyObject();
object b = MyObject.Clone();

Making changes to "a" has no effect on "b" because you now have two physical
objects in memory that share nothing (for the most part).

"Vincent" <ll**@hotmail.com> wrote in message
news:0b****************************@phx.gbl...
Hey, I have a problem to understand the underlaying of
clone method. Hope someone can help me out.

Let's say,
clonedObject = originalObject.clone()

(syntax may be wrong, but you know what I mean, I hope )

Why can't I just do this, instead?

clonedObject = originalObject

Any comment is more than welcome.
Thanks in advance.

Vincent

Jul 21 '05 #2
Vincent,

The Clone() method is supposed to create a copy (although any given class
may implement it somewhat differently than you might expect), which should
end up being a separate object with the same attributes and/or state as the
original object. The "clonedObject = originalObject" does not create a
copy. Instead, both variables end up as references to the same object.

HTH,
Nicole
"Vincent" <ll**@hotmail.com> wrote in message
news:0b****************************@phx.gbl...
Hey, I have a problem to understand the underlaying of
clone method. Hope someone can help me out.

Let's say,
clonedObject = originalObject.clone()

(syntax may be wrong, but you know what I mean, I hope )

Why can't I just do this, instead?

clonedObject = originalObject

Any comment is more than welcome.
Thanks in advance.

Vincent

Jul 21 '05 #3

Thank you. I have one more question. Is Clone() method the
same as create a new instance. Someone says clone() is
much faster than doing new(). Why and when should I use
clone() and new().

Thanks again.

A guy who want to learn.
Vincent

-----Original Message-----
Vincent,

The Clone() method is supposed to create a copy (although any given classmay implement it somewhat differently than you might expect), which shouldend up being a separate object with the same attributes and/or state as theoriginal object. The "clonedObject = originalObject" does not create acopy. Instead, both variables end up as references to the same object.
HTH,
Nicole
"Vincent" <ll**@hotmail.com> wrote in message
news:0b****************************@phx.gbl...
Hey, I have a problem to understand the underlaying of
clone method. Hope someone can help me out.

Let's say,
clonedObject = originalObject.clone()

(syntax may be wrong, but you know what I mean, I hope )

Why can't I just do this, instead?

clonedObject = originalObject

Any comment is more than welcome.
Thanks in advance.

Vincent

.

Jul 21 '05 #4
Vincent,

Performance will differ between classes since, amongst other things, the
implementation of their constructors and Clone methods will differ. That
said, for most classes, instantiating a new object should be faster than
cloning. The only generalized exception to this is when both objects do
need to have the same data and retrieving this data from a source other than
the potentially cloneable object is quite expensive (e.g.: a slow-running
database query).

In general, you should be using cloning very rarely (i.e.: only when cloning
is what you really need to do). If you just need to create a new object
that will happen to be similar to an existing object, there's no compelling
reason to using cloning.

Also, you should be aware that a Clone method might not really be performing
what you might consider to be "cloning". If the object implements the
ICloneable interface, it should, but even that's no guarantee. Before you
even consider using cloning, I'd recommend you at least read up on the
ICloneable interface and the MemberwiseClone method of the Object class.

HTH,
Nicole
"Vincent" <an*******@discussions.microsoft.com> wrote in message
news:04****************************@phx.gbl...

Thank you. I have one more question. Is Clone() method the
same as create a new instance. Someone says clone() is
much faster than doing new(). Why and when should I use
clone() and new().

Thanks again.

A guy who want to learn.
Vincent

-----Original Message-----
Vincent,

The Clone() method is supposed to create a copy (although

any given class
may implement it somewhat differently than you might

expect), which should
end up being a separate object with the same attributes

and/or state as the
original object. The "clonedObject = originalObject"

does not create a
copy. Instead, both variables end up as references to

the same object.

HTH,
Nicole
"Vincent" <ll**@hotmail.com> wrote in message
news:0b****************************@phx.gbl...
Hey, I have a problem to understand the underlaying of
clone method. Hope someone can help me out.

Let's say,
clonedObject = originalObject.clone()

(syntax may be wrong, but you know what I mean, I hope )

Why can't I just do this, instead?

clonedObject = originalObject

Any comment is more than welcome.
Thanks in advance.

Vincent

.

Jul 21 '05 #5

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

Similar topics

0
by: Jason Evans | last post by:
Hi All, I am writing my own implementation of queue via a linked list, note not a LinkedList, and was running into trouble with the clone method. I was wondering if anyone could point out some...
5
by: Chris | last post by:
Hi, I don't get the difference between a struct and a class ! ok, I know that a struct is a value type, the other a reference type, I understand the technical differences between both, but...
4
by: Vincent | last post by:
Hey, I have a problem to understand the underlaying of clone method. Hope someone can help me out. Let's say, clonedObject = originalObject.clone() (syntax may be wrong, but you know what I...
16
by: Bob Hairgrove | last post by:
Consider the classic clone() function: class A { public: virtual ~A() {} virtual A* clone() const = 0; }; class B : public A { public:
4
by: csharpula csharp | last post by:
Hello, Have a basic question about Clone() If I am using the clone method on some object ,are the Cloned object properties going to change at every property change in original object? Thanks! ...
16
by: Hamed | last post by:
Hello I am developing a utility to be reused in other programs. It I have an object of type Control (a TextBox, ComboBox, etc.) that other programmers use it in applications. they may set some...
14
by: Hamed | last post by:
Hello It seems that I should implement ICloneable to implement my own clone object. the critical point for me is to make a control object based on another control object that all of its event...
38
by: Zytan | last post by:
What is the difference between these two lines? Dim args As Object() = New Object() {strText} Dim args As Object() = {strText} args seems usuable from either, say, like so: ...
7
by: =?Utf-8?B?Sm9lbCBNZXJr?= | last post by:
I have created a custom class with both value type members and reference type members. I then have another custom class which inherits from a generic list of my first class. This custom listneeds...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.