Content deleted Content added
Cybercobra (talk | contribs) m Disambiguated: Pointer (computing) → Pointer (computer programming) |
m Improved formatting and some expressions (for your classes --> for the classes) |
||
Line 1:
In [[computer science]], '''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 most programming languages (exceptions include: [[Ruby (programming language)|Ruby]]), [[
Copying primitive types in Java:
Line 12:
</source>
Many OOP programming languages (including
A Java example, when "copying" an object using simple assignment:
Line 19:
Object copy = null;
copy = original; // does not copy object
</source>
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
Cloning an object in Java:
Line 34:
</source>
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|
A C++ example of object cloning:
Line 42:
</source>
A C++ example of object cloning using pointers (avoids slicing):<ref>See Q&A at [http://en.allexperts.com/q/C-1040/constructor-virtual.htm en.allexperts.com]</ref>
<source lang="cpp">
Object * original = new Object;
Line 53:
{{reflist}}
<!--Categories-->
[[Category:Object-oriented programming]]
<!--Interwikis-->
[[zh:克隆 (信息学)]]
|