Cloning (programming): Difference between revisions

Content deleted Content added
m changed int copy = null to int copy = 0, because the former is illegal in java 1.6 (and possibly earlier versions), also fixed a typo '<' becan '('
 
(35 intermediate revisions by 30 users not shown)
Line 1:
{{refimprove|date=August 2015}}
In [[computer science]], '''Cloningcloning''' refers to the making of an exact copy of an [[object (programming)|object]], frequently under the [[programming paradigm|paradigm]] of [[instance-based programming]], or [[object-oriented programming]]. (OOP).
 
==Shallow copies==
In allmost programming languages (exceptions include [[Ruby (programming language)|Ruby]]), variables[[primitive type]]s such as int<code>double</code>, <code>float</code>, double<code>int</code>, <code>long</code>, etc. simply store their values somewhere in the computer's memory (arbitrarily byoften the operating[[call systemstack]]). byBy using simple assignment, you can copy the contents of the variable to another one:
 
<pre>
Copying primitive types in Java or [[C++]]:
//Fig. 1 (Java Code)
<syntaxhighlight lang="java">
int original = 42;
int copy = 0;
 
//Copies contents original to copy (replaces copy's original value)
copy = original;
</syntaxhighlight>
</pre>
 
However, when "copying" objects this way, it does not actually "copy" the original. Eg:
Many OOP programming languages (including [[Java (programming language)|Java]], [[D (programming language)|D]], [[ECMAScript]], and [[C Sharp (programming language)|C#]]) make use of object references. Object references, which are similar to pointers in other languages, allow for objects to be passed around by [[Pointer (computer programming)|address]] so that the whole object need not be copied.
<pre>
 
//Fig. 2 (Java Code)
A Java example, when "copying" an object using simple assignment:
<syntaxhighlight lang="java">
Object original = new Object();
Object copy = null;
//NOT copying Object, but copying its reference
copy = original
</pre>
In object-oriented programming, everything is encapsulated in entities called objects. Objects can contain methods, which can be user defined or inheirited from their superclass. They can also contain variables holding any datatype, including their array types. Objects generally are created by using the popular new keyword:
<pre>
//Fig. 3 (Java Object)
Object myObj = new Object();
</pre>
This instantiates, or creates, an object and puts it into some piece of memory in the computer, and returns a reference to the instantiated object. So the variable myObj effectively holds a reference rather than the actual object itself. This is why the code in Fig. 2 copies only the reference, and not the actual object. So the variable original and copy actually point to the same thing! The reason why this is done is for efficiency. Instead of copying the entire object to assign it to the destination variable (which the object can get very large), and instead just juggle around references (which are simply integers in C/C++), it makes the distribution of the object around in a program (eg. passing it as an argument to a method), much more efficient.
 
copy = original; // does not copy object but only its reference
This process of passing references around is called shallow copying.
</syntaxhighlight>
 
The object is not duplicated, the variables 'original' and 'copy' are actually referring to the same object. In C++, the equivalent code
 
<syntaxhighlight lang="c">
Object* myObjoriginal = new Object();
Object* copy = NULL;
copy = original;
</syntaxhighlight>
 
makes it clear that it is a ''pointer'' to the object being copied, not the object itself.
==Cloning==
The process of actually making another exact replica of the object, instead of just its reference, is called cloning. In most languages, the language or libraries can facilitate some sort of cloning. In Java, the <code>Object</code> class contains the [[Clone (Java method)|<code>clone()</code> method]], which copies the object and returns a reference to that copied object. Since it is in the <code>Object</code> class, all classes defined in Java will have a clone method available to the programmer. In C++, a simple memcpy()although operationto (partfunction ofcorrectly theit Standardneeds Library)to willbe copyoverridden theat contentseach oflevel theit objectis intoused). a memory space, thus cloning the object.
 
Cloning an object in Java:
[[Category:Object-oriented programming]]
<syntaxhighlight lang="java">
Object originalObj = new Object();
Object copyObj = null;
 
copyObj = originalObj.clone(); // duplicates the object and assigns the new reference to 'copyObj'
[[zh:克隆 (信息学)]]
</syntaxhighlight>→
 
C++ objects in general behave like primitive types, so to copy a C++ object one could use the '<code>=</code>' (assignment) operator. There is a default assignment operator provided for all classes, but its effect may be altered through the use of [[operator overloading]]. There are dangers when using this technique (see [[Object slicing|slicing]]). A method of avoiding slicing can be implementing a similar solution to the Java <code>clone()</code> method for the classes and using pointers. (There is no built-in <code>clone()</code> method.)
 
A C++ example of object cloning:
<syntaxhighlight lang="cpp">
Object originalObj;
Object copyObj(originalObj); // creates a copy of originalObj named copyObj
</syntaxhighlight>
 
A C++ example of object cloning using pointers (to avoid slicing see<ref>See Q&A at [http://en.allexperts.com/q/C-1040/constructor-virtual.htm en.allexperts.com] {{Webarchive|url=https://web.archive.org/web/20090718130949/http://en.allexperts.com/q/C-1040/constructor-virtual.htm |date=2009-07-18 }}</ref>):
<syntaxhighlight lang="cpp">
Object* originalObj = new Object;
Object* copyObj = nullptr;
 
copyObj = new Object(*originalObj); // creates a copy of originalObj and assigns its address to copyObj
</syntaxhighlight>
 
==References==
{{reflist}}
 
<!--Categories-->
[[Category:Object-oriented programming]]