Cloning (programming)
Encyclopedia
In computer science
Computer science
Computer science or computing science is the study of the theoretical foundations of information and computation and of practical techniques for their implementation and application in computer systems...

, cloning refers to the making of an exact copy of an object, frequently under the paradigm
Programming paradigm
A programming paradigm is a fundamental style of computer programming. Paradigms differ in the concepts and abstractions used to represent the elements of a program and the steps that compose a computation A programming paradigm is a fundamental style of computer programming. (Compare with a...

 of instance-based programming, or object-oriented programming
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...

(OOP).

Shallow copies

In most programming languages (exceptions include: Ruby
Ruby (programming language)
Ruby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan during the mid-1990s and was first developed and designed by Yukihiro "Matz" Matsumoto...

), primitive types
Primitive type
In computer science, primitive data type is either of the following:* a basic type is a data type provided by a programming language as a basic building block...

 such as int, float, double, long, etc. simply store their values somewhere in the computer's memory (often the call stack
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, control stack, run-time stack, or machine stack, and is often shortened to just "the stack"...

). By using simple assignment, you can copy the contents of the variable to another one:

Copying primitive types in Java:

int original = 42;
int copy = 0;

copy = original;


Many OOP programming languages (including Java, C#, D
D (programming language)
The D programming language is an object-oriented, imperative, multi-paradigm, system programming language created by Walter Bright of Digital Mars. It originated as a re-engineering of C++, but even though it is mainly influenced by that language, it is not a variant of C++...

, ECMAScript
ECMAScript
ECMAScript is the scripting language standardized by Ecma International in the ECMA-262 specification and ISO/IEC 16262. The language is widely used for client-side scripting on the web, in the form of several well-known dialects such as JavaScript, JScript, and ActionScript.- History :JavaScript...

) make use of object references. Object references, which are similar to pointers in other languages, allow for objects to be passed around by address 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, only copies its reference

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
Clone (Java method)
clone is a method in the Java programming language for object duplication. In Java, objects are manipulated through reference variables, and there is no operator for copying an object—the assignment operator duplicates the reference, not the object...

, 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 original = new Object;
Object copy = null;

copy = original.clone; // duplicates the object and assigns the new reference to 'copy'


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
Operator overloading
In object oriented computer programming, operator overloading—less commonly known as operator ad-hoc polymorphism—is a specific case of polymorphism, where different operators have different implementations depending on their arguments...

. 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:

Object original;
Object copy(original); // creates a copy of original named copy


A C++ example of object cloning using pointers (avoids slicing):

Object * original = new Object;
Object * copy = NULL;

copy = new Object(* original); // creates a copy of original and assigns its address to copy
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK