Cloning (programming)

Cloning (programming)

In computer science, cloning refers to the making of an exact copy of an object, frequently under the paradigm of instance-based programming, or object-oriented programming(OOP).

Shallow copies

In most programming languages (exceptions include: Ruby), primitive types such as int, float, double, long, etc. simply store their values somewhere in the computer's memory (often the call 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, ECMAScript) 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, 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. 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)[1]:

Object * original = new Object;
Object * copy = NULL;
 
copy = new Object(* original); // creates a copy of original and assigns its address to copy

References

  1. ^ See Q&A at en.allexperts.com

Wikimedia Foundation. 2010.

Игры ⚽ Поможем сделать НИР

Look at other dictionaries:

  • Cloning (disambiguation) — Cloning is the process of making an identical copy of something. Cloning may also refer to: Cloning (programming), the copying of a programming object Disk cloning, the copying of the contents of a computer hard disk to a storage medium or file… …   Wikipedia

  • Programming paradigm — Programming paradigms Agent oriented Automata based Component based Flow based Pipelined Concatenative Concu …   Wikipedia

  • Object-oriented programming — Programming paradigms Agent oriented Automata based Component based Flow based Pipelined Concatenative Concurrent computing …   Wikipedia

  • Comparison of programming languages (object-oriented programming) — Programming language comparisons General comparison Basic syntax Basic instructions Arrays Associative arrays String operations …   Wikipedia

  • Constructor (object-oriented programming) — Programming language comparisons General comparison Basic syntax Basic instructions Arrays Associative arrays String operations …   Wikipedia

  • Prototype-based programming — is a style of object oriented programming in which classes are not present, and behavior reuse (known as inheritance in class based languages) is performed via a process of cloning existing objects that serve as prototypes. This model can also be …   Wikipedia

  • Agora (programming language) — Agora is a reflective, prototype based, object oriented programming language that is based exclusively on message passing and not delegation. Agora was intended to show that even subject to that limit, it is possible to build a full object… …   Wikipedia

  • Multi-paradigm programming language — A multi paradigm programming language is a programming language that supports more than one programming paradigm. As Leda designer Tim Budd holds it: The idea of a multiparadigm language is to provide a framework in which programmers can work in… …   Wikipedia

  • Quantum programming — is a set of computer programming languages that allow the expression of quantum algorithms using high level constructs. The point of quantum languages is not so much to provide a tool for programmers, but to provide tools for researchers to… …   Wikipedia

  • Lua (programming language) — Infobox programming language name = Lua paradigm = Multi paradigm: scripting, imperative, functional year = 1993 designer = Roberto Ierusalimschy Waldemar Celes Luiz Henrique de Figueiredo developer = latest release version = 5.1.4 latest release …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”