HOME

TheInfoList



OR:

In
computer science Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to practical disciplines (includin ...
, cloning refers to the making of an exact copy of an
object Object may refer to: General meanings * Object (philosophy), a thing, being, or concept ** Object (abstract), an object which does not exist at any particular time or place ** Physical object, an identifiable collection of matter * Goal, an ai ...
, frequently under the paradigm of
instance-based programming Prototype-based programming is a style of object-oriented programming in which behaviour reuse (known as inheritance) is performed via a process of reusing existing objects that serve as prototypes. This model can also be known as ''prototypal' ...
, or
object-oriented programming Object-oriented programming (OOP) is a programming paradigm based on the concept of " objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of ...
(OOP).


Shallow copies

In most programming languages (exceptions include:
Ruby A ruby is a pinkish red to blood-red colored gemstone, a variety of the mineral corundum (aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sapp ...
),
primitive type In computer science, primitive data types are a set of basic data types from which all other data types are constructed. Specifically it often refers to the limited set of data representations in use by a particular processor, which all compiled p ...
s such as double, float, int, long, etc. simply store their values somewhere in the computer's memory (often the
call stack In computer science, a call stack is a stack data structure that stores information about the active subroutines of a computer program. This kind of stack is also known as an execution stack, program stack, control stack, run-time stack, or mach ...
). By using simple assignment, you can copy the contents of the variable to another one: Copying primitive types in Java or C++: int original = 42; int copy = 0; copy = original; Many OOP programming languages (including
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mo ...
, D,
ECMAScript ECMAScript (; ES) is a JavaScript standard intended to ensure the interoperability of web pages across different browsers. It is standardized by Ecma International in the documenECMA-262 ECMAScript is commonly used for client-side scriptin ...
, and C#) make use of object references. Object references, which are similar to pointers in other languages, allow for objects to be passed around by
address An address is a collection of information, presented in a mostly fixed format, used to give the location of a building, apartment, or other structure or a plot of land, generally using political boundaries and street names as references, along ...
so that the whole object need not be copied. A Java example, when "copying" an object using simple assignment: Object original = new Object(); Object copy = null; copy = original; // does not copy object but only its reference The object is not duplicated, the variables 'original' and 'copy' are actually referring to the same object. In C++, the equivalent code Object* original = new Object(); Object* copy = NULL; copy = original; 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 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 (although to function correctly it needs to be overridden at each level it is used). Cloning an object in Java: Object originalObj = new Object(); Object copyObj = null; copyObj = originalObj.clone(); // duplicates the object and assigns the new reference to 'copyObj' 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 In computer programming, operator overloading, sometimes termed ''operator ad hoc polymorphism'', is a specific case of polymorphism, where different operators have different implementations depending on their arguments. Operator overloading i ...
. 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 the classes and using pointers. (Note that there is no built-in clone() method) A C++ example of object cloning: Object originalObj; Object copyObj(originalObj); // creates a copy of originalObj named copyObj A C++ example of object cloning using pointers (to avoid slicing see See Q&A a
en.allexperts.com
): Object* originalObj = new Object; Object* copyObj = nullptr; copyObj = new Object(*originalObj); // creates a copy of originalObj and assigns its address to copyObj


References

{{reflist Object-oriented programming