Cloning (programming): Difference between revisions

Content deleted Content added
thorough modifications, eliminated the use of memcpy in C++ (non-standard), juggled around examples, removed info that was incomplete paraphrasing of OOP article
 
(34 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 most programming languages (exceptions include: [[Ruby (programming language)|Ruby]]), [[Primitiveprimitive type|primitive types]]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 (often the [[Call stack|call stack]]). By using simple assignment, you can copy the contents of the variable to another one:
 
Copying primitive types in Java or [[C++]]:
<syntaxhighlight lang="java">
<pre>
int original = 42;
int copy = 0;
 
copy = original;
</syntaxhighlight>
</pre>
 
Many OOP programming languages (including Java, [[CJava Sharp(programming language)|C#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 (computingcomputer programming)|address]] so that the whole object need not be copied.
 
A Java example, when "copying" an object using simple assignment:
<syntaxhighlight lang="java">
<pre>
Object original = new Object();
Object copy = null;
 
copy = original; // does not copy object, onlybut copiesonly its reference
</syntaxhighlight>
</pre>
The object is not duplicated, the variables 'original' and 'copy' are actually referring to the same object.
 
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 * original = 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 (although to function correctly it needs to be overridden at each level it is used).
 
Cloning an object in Java:
<syntaxhighlight lang="java">
<pre>
Object originaloriginalObj = new Object();
Object copycopyObj = null;
 
copycopyObj = originaloriginalObj.clone(); // duplicates the object and assigns the new reference to 'copycopyObj'
</syntaxhighlight>→
</pre>
 
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 [[SlicingObject slicing|slicing]]). A method of avoiding slicing can be implementing a similar solution to the Java <code>clone()</code> method for yourthe classes, and using pointers. (Note that thereThere is no built -in <code>clone()</code> method.)
 
A C++ example of object cloning:
<syntaxhighlight lang="cpp">
<pre>
Object originaloriginalObj;
Object copyObj(originalObj); // creates a copy of originalObj named copyObj
Object copy;
</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>):
copy = original; // copies the contents of original into copy
<syntaxhighlight lang="cpp">
</pre>
Object* originalObj = new Object;
Object* copyObj = nullptr;
 
copycopyObj = original.clonenew Object(*originalObj); // creates a copy of originaloriginalObj and assigns its address to copycopyObj
A C++ example of object cloning using pointers (avoids slicing):
</syntaxhighlight>
<pre>
Object * original = new Object;
Object * copy = NULL;
 
==References==
copy = original.clone(); // creates a copy of original and assigns its address to copy
{{reflist}}
</pre>
 
<!--Categories-->
[[Category:Object-oriented programming]]
 
[[zh:克隆 (信息学)]]