Object Copy Article Index for
Object
Website Links For
Object
 

Information About

Object Copy




In Computing , a deep copy is a copy that contains the complete encapsulated data of the original object, allowing it to be used independently of the original object.
In contrast, a ''shallow copy'' is a copy that may be associated to data shared by the original and the copy.

For example, if a C++ class contains a pointer to a null-terminated string, the deep copy would also copy the string, while the shallow copy would create an object where the pointer points to same string, and changes to it affect both objects.

Shallow copies are common when Reference Counting objects.



In Computer Science , duplication of data or objects involves copying data byte by byte and, if needed, changing metadata about objects.


DUPLICATION OF DATA


In C , one can use memmove or memcpy to copy data on the memory.


DUPLICATION OF OBJECTS


A built-in method called a Copy Constructor can be used to easily duplicate most objects. Programmers typically implement the copy constructor as a recursive copy of all attributes and of all objects belonging to the object, one-by-one. This can take a lot of time. The duplication of List s, for instance, involves copying each element.

Implementors may set up some copy constructors with :


#include
#include
using namespace std;

int main()
{
string s("abc");
string t;
char & c(s {Link without Title} );

t = s; // Data typically shared between s and t.
c = 'z'; // How many strings does this modify?
if (t {Link without Title} == 'z')
cout << "wrong" << endl;
else
cout << "right" << endl;
}


The specification of C++ understandably frowns on changing t. One can only mitigate this problem by the use of defensive copy, but this in turn wipes out any benefit gained by the use of Reference Counting .