473,763 Members | 6,666 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2279
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.c om> 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 "clonedObje ct = originalObject" does not create a
copy. Instead, both variables end up as references to the same object.

HTH,
Nicole
"Vincent" <ll**@hotmail.c om> 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 "clonedObje ct = originalObject" does not create acopy. Instead, both variables end up as references to the same object.
HTH,
Nicole
"Vincent" <ll**@hotmail.c om> 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*******@disc ussions.microso ft.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 "clonedObje ct = originalObject"

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

the same object.

HTH,
Nicole
"Vincent" <ll**@hotmail.c om> 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
2669
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 not so obvious errors I have made.. Cheers
5
8732
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 conceptually speaking : when do I define something as 'struct' and when as 'class' ? for example : if I want to represent a 'Time' thing, containing : - data members : hours, mins, secs
4
386
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 mean, I hope ) Why can't I just do this, instead?
16
3204
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
2531
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! *** Sent via Developersdex http://www.developersdex.com ***
16
2516
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 of properties or assign event handlers. I need to be able to clone the manipulated control at runtime. I could use the Clone method of some objects (like Font, Style, String,
14
8639
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 handlers are set like the old one. Is there a way to do this job? For example, is there a way to use EventInfo object to get all event handlers of the old control in runtime and set my new cloned control events to the event handlers of the old...
38
2326
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: Me.Invoke(delegate, args) Zytan
7
2844
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 to support cloning: Public Class RefClass Public tcp As TcpClient Public name As String End Class Public Class RefClassList Inherits List(Of RefClass) Implements ICloneable
0
9564
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9387
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10148
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8822
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6643
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3528
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2794
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.