Cloning (programming)

Last updated

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

Contents

Shallow copies

In most programming languages (exceptions include Ruby), primitive types such as double, float, int, 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 or C++:

intoriginal=42;intcopy=0;copy=original;

Many OOP programming languages (including Java, D, ECMAScript, 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 so that the whole object need not be copied.

A Java example, when "copying" an object using simple assignment:

Objectoriginal=newObject();Objectcopy=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=newObject();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:

ObjectoriginalObj=newObject();ObjectcopyObj=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. 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. (There is no built-in clone() method.)

A C++ example of object cloning:

ObjectoriginalObj;ObjectcopyObj(originalObj);// creates a copy of originalObj named copyObj

A C++ example of object cloning using pointers (to avoid slicing see [1] ):

Object*originalObj=newObject;Object*copyObj=nullptr;copyObj=newObject(*originalObj);// creates a copy of originalObj and assigns its address to copyObj

Related Research Articles

In programming languages, a closure, also lexical closure or function closure, is a technique for implementing lexically scoped name binding in a language with first-class functions. Operationally, a closure is a record storing a function together with an environment. The environment is a mapping associating each free variable of the function with the value or reference to which the name was bound when the closure was created. Unlike a plain function, a closure allows the function to access those captured variables through the closure's copies of their values or references, even when the function is invoked outside their scope.

In object-oriented and functional programming, an immutable object is an object whose state cannot be modified after it is created. This is in contrast to a mutable object, which can be modified after it is created. In some cases, an object is considered immutable even if some internally used attributes change, but the object's state appears unchanging from an external point of view. For example, an object that uses memoization to cache the results of expensive computations could still be considered an immutable object.

In C++ programming, object slicing occurs when an object of a subclass type is copied to an object of superclass type: the superclass copy will not have any of the member variables or Member functions defined in the subclass. These variables and functions have, in effect, been "sliced off".

In object-oriented programming, object copying is creating a copy of an existing object, a unit of data in object-oriented programming. The resulting object is called an object copy or simply copy of the original object. Copying is basic but has subtleties and can have significant overhead. There are several ways to copy an object, most commonly by a copy constructor or cloning. Copying is done mostly so the copy can be modified or moved, or the current value preserved. If either of these is unneeded, a reference to the original data is sufficient and more efficient, as no copying occurs.

In computer science, boxing is the transformation of placing a primitive type within an object so that the value can be used as a reference. Unboxing is the reverse transformation of extracting the primitive value from its wrapper object. Autoboxing is the term for automatically applying boxing and/or unboxing transformations as needed.

<span class="mw-page-title-main">Pointer (computer programming)</span> Object which stores memory addresses in a computer program

In computer science, a pointer is an object in many programming languages that stores a memory address. This can be that of another value located in computer memory, or in some cases, that of memory-mapped computer hardware. A pointer references a location in memory, and obtaining the value stored at that location is known as dereferencing the pointer. As an analogy, a page number in a book's index could be considered a pointer to the corresponding page; dereferencing such a pointer would be done by flipping to the page with the given page number and reading the text found on that page. The actual format and content of a pointer variable is dependent on the underlying computer architecture.

In computer programming, a function object is a construct allowing an object to be invoked or called as if it were an ordinary function, usually with the same syntax. Function objects are often called functors.

This article compares two programming languages: C# with Java. While the focus of this article is mainly the languages and their features, such a comparison will necessarily also consider some features of platforms and libraries. For a more detailed comparison of the platforms, see Comparison of the Java and .NET platforms.

In class-based, object-oriented programming, a constructor is a special type of function called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required member variables.

<span class="mw-page-title-main">Java syntax</span> Set of rules defining correctly structured program

The syntax of Java is the set of rules defining how a Java program is written and interpreted.

In some programming languages, const is a type qualifier that indicates that the data is read-only. While this can be used to declare constants, const in the C family of languages differs from similar constructs in other languages in being part of the type, and thus has complicated behavior when combined with pointers, references, composite data types, and type-checking. In other languages, the data is not in a single memory location, but copied at compile time on each use. Languages which utilize it include C, C++, D, JavaScript, Julia, and Rust.

<span class="mw-page-title-main">C Sharp (programming language)</span> Programming language

C# is a general-purpose high-level programming language supporting multiple paradigms. C# encompasses static typing, strong typing, lexically scoped, imperative, declarative, functional, generic, object-oriented (class-based), and component-oriented programming disciplines.

In the Java programming language, the final keyword is used in several contexts to define an entity that can only be assigned once.

In computer programming, an opaque pointer is a special case of an opaque data type, a data type declared to be a pointer to a record or data structure of some unspecified type.

The syntax of JavaScript is the set of rules that define a correctly structured JavaScript program.

this, self, and Me are keywords used in some computer programming languages to refer to the object, class, or other entity of which the currently running code is a part. The entity referred to by these keywords thus depends on the execution context. Different programming languages use these keywords in slightly different ways. In languages where a keyword like "this" is mandatory, the keyword is the only way to access data and methods stored in the current object. Where optional, they can disambiguate variables and functions with the same name.

C++11 is a version of the ISO/IEC 14882 standard for the C++ programming language. C++11 replaced the prior version of the C++ standard, called C++03, and was later replaced by C++14. The name follows the tradition of naming language versions by the publication year of the specification, though it was formerly named C++0x because it was expected to be published before 2010.

Nullable types are a feature of some programming languages which allow a value to be set to the special value NULL instead of the usual possible values of the data type. In statically typed languages, a nullable type is an option type, while in dynamically typed languages, equivalent behavior is provided by having a single null value.

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. The clone method provides this missing functionality.

The computer programming language, C#, introduces several new features in version 2.0. These include:

References

  1. See Q&A at en.allexperts.com Archived 2009-07-18 at the Wayback Machine