encyclopegia.org

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

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 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++: int original = 42; int copy = 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: 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. 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: Object originalObj; Object copyObj(originalObj); // creates a copy of originalObj named copyObj A C++ example of object cloning using pointers (to avoid slicing see ): Object* originalObj = new Object; Object* copyObj = nullptr; copyObj = new Object(*originalObj); // creates a copy of originalObj and assigns its address to copyObj ==References==

References

  1. See Q&A at en.allexperts.com Archived 2009-07-18 at the Wayback Machine Wikipedia revision 1149318006, retrieved 2026-11-19 · retrieved 2026-11-18 · rev 1149318006
    Cite this reference

    MLA "See Q&A at en.allexperts.com Archived 2009-07-18 at the Wayback Machine Wikipedia revision 1149318006, retrieved 2026-11-19." See Q&A at en. 2026-11-18. http://en.allexperts.com/q/C-1040/constructor-virtual.htm. Accessed 24 November 2026.

    Chicago "See Q&A at en.allexperts.com Archived 2009-07-18 at the Wayback Machine Wikipedia revision 1149318006, retrieved 2026-11-19." See Q&A at en. 2026-11-18. http://en.allexperts.com/q/C-1040/constructor-virtual.htm.

    APA See Q&A at en. (2026). See Q&A at en.allexperts.com Archived 2009-07-18 at the Wayback Machine Wikipedia revision 1149318006, retrieved 2026-11-19. Retrieved November 24, 2026, from http://en.allexperts.com/q/C-1040/constructor-virtual.htm

Cite this article

MLA

"Cloning (programming)." encyclopegia.org, 2023, https://encyclopegia.org/wiki/cloning-programming. Accessed 24 November 2026.

Chicago

"Cloning (programming)." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/cloning-programming.

APA

encyclopegia.org. (2023). Cloning (programming). Retrieved November 24, 2026, from https://encyclopegia.org/wiki/cloning-programming

Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON

Related articles

Miracle Mile, Los Angeles

enwiki · references available

Leuciscus

enwiki · references available

Yilan City

enwiki · references available

Poley McClintock

enwiki · references available

Bjelovar-Križevci County

enwiki · references available

Lika-Krbava County

enwiki · references available

First Baptist Church (Columbus, Indiana)

enwiki · references available

Constant-velocity joint

enwiki · references available

Sandkings (short story collection)

enwiki · references available

Victor Desguin

enwiki · references available
CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1149318006 · 0.5 · snapshot enwiki-001149318006

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.