Object copy
From Wikipedia, the free encyclopedia
One of the most common procedures that occurs in computer programs is the copying of data. An object is a composite data type in object-oriented programming languages. Object copy thus describes the action wherein an object has its attributes copied to another object of the same data type. An object may be copied in order to reuse all or part of its data in a new context.
Contents |
[edit] How to copy objects
The programming procedure for copying objects varies from language to language. The simplest method is to use the assignment operator "=". This option exists in nearly all, if not all, programming languages. The following example shows how to do this in C++.
// definition of how the object Vector looks like class Vector { public: int x, y, z; }; . . . Vector A, B; A.x = 10; A.y = 20; A.z = 30; B = A; // copy A into B |
In the preceding example, the values of all attributes of object A were copied into corresponding attributes in object B. This technique is fine as long as there is no need for control over which attributes are copied and which not. To overcome this inconvenience some object-oriented programming languages offer a feature called operator overloading.
Another way how to make a copy of an object is to do it at it's creation. This approach documents following excerpt.
// definition of how the object Vector looks like class Vector { public: int x, y, z; }; . . . Vector A; A.x = 10; A.y = 20; A.z = 30; Vector B(A); // copy A into B at B's creation
In this case, an implicit copy constructor is used to achieve the same result as in previous example. If a programmer wants to specify what attributes are to be copied, he or she can write explicit copy constructor.
[edit] When object copying occurs
If we don't define an explicit copy constructor, C++ will create the default [[copy
- When an object is passed by value.
- When a function returns an object.
Example:Cat makeCat(char *name) { Cat uk(name); // Creates the instance uk return uk; // Copy constructor is used to // return the instance uk }
- When an object is a parametr of a function.
Example:void takeCat(Cat c) { // In this case, a copy constructor is used to create a local copy // of the object. }
Also, when using operator "=" (assignment) the object copying occurs. This requires the assignment operator (operator=) to be overloaded, or else only shallow copy is performed.
Example:
Cat a; Cat b; a = b; // Object copy using the assignment operator
[edit] Deep vs. Shallow 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.
[edit] Deep copy implementation
If you want to implement a deep copy you generally need to implement these 4 methods to ensure correct data transfer during call of copy constructor or assignment operator.
- Constructor (computer science) - Here you allocate your dynamic data structures
- Copy constructor - Here you allocate your dynamic data structures and copy to them data from the object, that you get
- Overload assignment operator - Here you reallocate your dynamic data structures and copy to them data from the object, that you get
- Destructor (computer science) - Here you delete all your allocated data structures
[edit] 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 lists, for instance, involves copying each element.
Implementors may set up some copy constructors with reference counting so that identical copies of an object do not take up additional memory. This technique interacts badly with low-level access to internal object data. Defensive copy is an act to ensure that the object retains a state that exists at the moment of duplication.
[edit] See also
[edit] References
- Why Copying an Object is a terrible thing to do? This article addresses the issue of copying objects and the correct way to write the code for copying objects.