Object copying

Uses your system voice; voice and rate in settings.

Snapshot

≈ 4 min read

Names in other languages

No names in other languages are held for this entry yet.

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

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. Objects in general store composite data. While in simple cases copying can be done by allocating a new, uninitialized object and copying all fields (attributes) from the original object, in more complex cases this does not result in desired behavior.

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. Objects in general store composite data. While in simple cases copying can be done by allocating a new, uninitialized object and copying all fields (attributes) from the original object, in more complex cases this does not result in desired behavior. The Eiffel class ANY contains features for shallow and deep copying and cloning of objects. All Eiffel classes inherit from ANY , so these features are available within all classes, and are applicable both to reference and expanded objects. The copy feature effects a shallow, field-by-field copy from one object to another. In this case no new object is created. If y were copied to x , then the same objects referenced by y before the application of copy , will also be referenced by x after the copy feature completes. To effect the creation of a new object which is a shallow duplicate of y , the feature twin is used. In this case, one new object is created with its fields identical to those of the source. The feature twin relies on the feature copy , which can be redefined in descendants of ANY , if needed. The result of twin is of the anchored type like Current . Deep copying and creating deep twins can be done using the features deep_copy and deep_twin , again inherited from class ANY . These features have the potential to create many new objects, because they duplicate all the objects in an entire object structure. Because new duplicate objects are created instead of simply copying references to existing objects, deep operations will become a source of performance issues more readily than shallow operations. In Perl, nested structures are stored by the use of references, thus a developer can either loop over the entire structure and re-reference the data or use the dclone() function from the module Storable. In VBA, an assignment of variables of type Object is a shallow copy, an assignment for all other types (numeric types, String, user defined types, arrays) is a deep copy. So the keyword Set for an assignment signals a shallow copy and the (optional) keyword Let signals a deep copy. There is no built-in method for deep copies of Objects in VBA.

Notes

Text derived from https://en.wikipedia.org/w/index.php?oldid=1364772801 under CC BY-SA 4.0; modifications have been made.

Provenance

snapshot enwiki-001364772801 · captured 1784372044.0 · version history

Cite this page as: encyclopegia.org, “Object copying”, snapshot enwiki-001364772801. https://encyclopegia.org/a/object-copying

Sources cited in this revision

12 named source(s) extracted from the cited revision. A source with no author is shown by its publication; nothing is invented.