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 '('
thorough modifications, eliminated the use of memcpy in C++ (non-standard), juggled around examples, removed info that was incomplete paraphrasing of OOP article
Line 1:
'''Cloning''' 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|primitive types]] such as int, float, double, long, etc. simply store their values somewhere in the computer's memory (arbitrarily byoften the operating[[Call stack|call systemstack]]). byBy using simple assignment, you can copy the contents of the variable to another one:
 
Copying primitive types in Java:
<pre>
//Fig. 1 (Java Code)
int original = 42;
int copy = 0;
 
//Copies contents original to copy (replaces copy's original value)
copy = original;
</pre>
 
However, when "copying" objects this way, it does not actually "copy" the original. Eg:
Many OOP programming languages (including Java, [[C Sharp|C#]], [[D (programming language)|D]], [[ECMAScript]]) make use of object references. Object references, which are similar to pointers in other languages, allow for objects to be passed around by [[Pointer (computing)|address]] so that the whole object need not be copied.
 
A Java example, when "copying" an object using simple assignment:
<pre>
//Fig. 2 (Java Code)
Object original = new Object();
Object copy = null;
 
//NOT copying Object, but copying its reference
copy = original; // does not copy object, only copies its reference
</pre>
The object is not duplicated, the variables 'original' and 'copy' are actually referring to the same object.
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:
 
==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 Object class contains the clone() method, which copies the object and returns a reference to that copied object. Since it is in the Object 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:
<pre>
//Fig.Object 3original (Java= new Object();
Object myObjcopy = new Object()null;
 
copy = original.clone(); // duplicates the object and assigns the new reference to 'copy'
</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.
 
C++ objects in general behave like primitive types, so to copy a C++ object one could use the '=' (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 [[Slicing]]). A method of avoiding slicing can be implementing a similar solution to the Java clone() method for your classes, and using pointers. (Note that there is no built in clone() method)
This process of passing references around is called shallow copying.
 
A C++ example of object cloning:
==Cloning==
<pre>
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 Object class contains the clone() method, which copies the object and returns a reference to that copied object. Since it is in the Object class, all classes defined in Java will have a clone method available to the programmer. In C++, a simple memcpy() operation (part of the Standard Library) will copy the contents of the object into a memory space, thus cloning the object.
Object original;
Object copy;
 
copy = original; // copies the contents of original into copy
</pre>
 
A C++ example of object cloning using pointers (avoids slicing):
<pre>
Object * original = new Object;
Object * copy = NULL;
 
copy = original.clone(); // creates a copy of original and assigns its address to copy
</pre>
 
[[Category:Object-oriented programming]]