Content deleted Content added
StuartMurray (talk | contribs) 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 '(' |
StuartMurray (talk | contribs) 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
Copying primitive types in Java:
<pre>
int original = 42;
int copy = 0;
copy = original;
</pre>
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>
Object original = new Object();
Object copy = null;
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.
==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
Cloning an object in Java:
<pre>
Object
copy = original.clone(); // duplicates the object and assigns the new reference to 'copy'
</pre>
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)
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]]
|