Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Object copying
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Methods of copying == The design goal of most objects is to give the resemblance of being made out of one monolithic block even though most are not. As objects are made up of several different parts, copying becomes nontrivial. Several strategies exist to treat this problem. Consider an object A, which contains fields x<sub>i</sub> (more concretely, consider if A is a string and x<sub>i</sub> is an array of its characters). There are different strategies for making a copy of A, referred to as ''shallow copy'' and ''deep copy''. Many languages allow generic copying by one or either strategy, defining either one ''copy'' operation or separate ''shallow copy'' and ''deep copy'' operations.{{sfn|Grogono|Sakkinen|2000}} Note that even shallower is to use a [[reference (computer science)|reference]] to the existing object A, in which case there is no new object, only a new reference. The terminology of ''shallow copy'' and ''deep copy'' dates to [[Smalltalk]]-80.{{sfn|Goldberg|Robson|1983|pages=97β99|ps=. "There are two ways to make copies of an object. The distinction is whether or not the values of the object's variables are copied. If the values are not copied, then they are shared (<code>shallowCopy</code>); if the values are copied, then they are not shared (<code>deepCopy</code>)."}} The same distinction holds for comparing objects for equality: most basically there is a difference between identity (same object) and equality (same value), corresponding to shallow equality and (1 level) deep equality of two object references, but then further whether equality means comparing only the fields of the object in question or dereferencing some or all fields and comparing their values in turn (e.g., are two linked lists equal if they have the same nodes, or if they have same values?).{{Clarify|date=November 2017}} === Shallow copy === {{multiple image | align = right | image1 = 49psq.png | width1 = 135 | alt1 = A and B refer to different areas in memory. | caption1 = Variable reference to different memory space | image2 = QqE2L.png | width2 = 135 | alt2 = The assignment of variable B to A. | caption2 = The assignment of variable B to A. | footer = | image3 = Cys27.png | width3 = 135 | alt3 = Variables referring to same area of memory. | caption3 = Variables referring to same area of memory. }} One method of copying an object is the ''shallow copy''. In that case a new object B is [[object creation|created]], and the fields values of A are copied over to B.<ref>{{cite web|title=C++ Shallow vs Deep Copy Explanation|url=http://www.fredosaurus.com/notes-cpp/oop-condestructors/shallowdeepcopy.html}}</ref><ref>{{cite web|title=.NET Shallow vs Deep Copy Explanation|url=http://www.codeproject.com/Articles/28952/Shallow-Copy-vs-Deep-Copy-in-NET}}</ref><ref>{{cite web|title=Generic Shallow vs Deep Copy Explanation|url=https://secweb.cs.odu.edu/~zeil/cs361/web/website/Lectures/big3/pages/shallowvsdeep.html|access-date=2013-04-10|archive-url=https://web.archive.org/web/20160304115828/https://secweb.cs.odu.edu/~zeil/cs361/web/website/Lectures/big3/pages/shallowvsdeep.html|archive-date=2016-03-04|url-status=dead}}</ref> This is also known as a ''field-by-field copy'',<ref>Core Java: Fundamentals, Volume 1, [https://books.google.com/books?id=QTZvAQAAQBAJ&pg=PA295&dq=%22field+copy%22 p. 295]</ref><ref>''[[Effective Java]]'', Second Edition, [https://books.google.com/books?id=ka2VUBqHiWkC&pg=PA54&dq=%22field+copy%22 p. 54]</ref><ref>"[https://stackoverflow.com/questions/2890340/what-is-this-field-by-field-copy-done-by-object-clone What is this field-by-field copy done by Object.clone()?]", ''Stack Overflow''</ref> ''field-for-field copy'', or ''field copy''.<ref>"Josh Bloch on Design: A Conversation with Effective Java Author, Josh Bloch", by Bill Venners, ''JavaWorld'', January 4, 2002, [http://www.artima.com/intv/bloch13.html p. 13]</ref> If the field value is a reference to an object (e.g., a memory address) it copies the reference, hence referring to the same object as A does, and if the field value is a primitive type, it copies the value of the primitive type. In languages without primitive types (where everything is an object), all fields of the copy B are references to the same objects as the fields of original A. The referenced objects are thus ''shared'', so if one of these objects is modified (from A or B), the change is visible in the other. Shallow copies are simple and typically cheap, as they can usually be implemented by simply copying the bits exactly. === Deep copy === {{multiple image | align = right | image1 = deep copy in progress.svg | width1 = 200 | alt1 = A deep copy in progress. | caption1 = A deep copy in progress. | image2 = deep copy done.svg | width2 = 200 | alt2 = A deep copy has been completed | caption2 = A deep copy has been completed. | footer = }} An alternative is a deep copy, meaning that fields are dereferenced: rather than references to objects being copied, new copy of objects are created for any referenced objects, and references to these are placed in B. Later modifications to the contents of either remain unique to A or B, as the contents are not shared. === Combination === In more complex cases, some fields in a copy should have shared values with the original object (as in a shallow copy), corresponding to an "association" relationship; and some fields should have copies (as in a deep copy), corresponding to an "aggregation" relationship. In these cases a custom implementation of copying is generally required; this issue and solution dates to Smalltalk-80.{{sfn|Goldberg|Robson|1983|page=97 |ps=. "The default implementation of <code>copy</code> is <code>shallowCopy</code>. In subclasses in which copying must result in a special combination of shared and unshared variables, the method associated with copy is usually reimplemented, rather than the method associated with <code>shallowCopy</code> or <code>deepCopy</code>."}} Alternatively, fields can be marked as requiring a shallow copy or deep copy, and copy operations automatically generated (likewise for comparison operations).{{sfn|Grogono|Sakkinen|2000}} This is not implemented in most object-oriented languages, however, though there is partial support in Eiffel.{{sfn|Grogono|Sakkinen|2000}}
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)