Under what conditions should I use the shortest copy function? When should I use the deep copy function?

Source: Internet
Author: User
Q: What is shallow copy and deep copy )?
 
A:
 
A shallow copy is a one-to-one value assignment between member data: assign values to the values to be copied. However, there may be a situation where the object also contains resources. The resources here can be a heap resource or a file .. When the value is copied, the two objects use the same resources and can access the resources, which leads to problems. Deep copy is used to solve this problem. It also assigns resources once so that objects have different resources, but the content of resources is the same. For heap resources, a heap memory is being opened to copy the original content.
 
If the object you copied references an external content (such as the Data allocated to the stack), let the new and old objects point to the same external content when copying the object, it is a shallow copy. If an independent copy of an external object is created for the new object when the object is copied, It is a deep copy.
 
The semantics of a reference is similar to that of a pointer. A reference is an unchangeable pointer and a pointer is a changeable reference. The reference semantics is actually implemented.
The difference between deep copy and shallow copy is that when the object State contains references from other objects, when copying an object, if you need to copy the object referenced by this object, it is a deep copy, otherwise, it is a shortest copy.
 
Cow semantics is a combination of "Deep copy" and "Deferred computing". It is still a deep copy, not a shallow copy, because the data of the two objects after the copy is logically irrelevant, the content is the same.
 
========================================================== ==========================================
 
Q: under what circumstances will deep copy be used when the light copy function is used?
 
A:
 
It is required regardless of the depth. When a deep copy occurs, it usually indicates that there is an "aggregation relationship", while when a shallow copy occurs, it usually indicates that there is a "acquaintance relationship ".
A simple example:
When you implement a composite pattern, you usually implement a deep copy (if you need to copy it), and few require the same composite to share leaf;
When you implement an observer pattern, if you need to copy the observer, you probably won't copy the subject, then you need to implement a shortest copy.
Whether it is a deep copy or a shallow copy does not depend on time efficiency, space efficiency, or language, but on which one is logically correct.
 
========================================================== ============================================
 
Q: In C ++, how does the default constructor copy objects?
 
A:
 
Correct another concept: the default constructor is not responsible for the copy action. I think you should be referring to implicitly-declared copy constructor. It will call copy constructor of all immediate base classes and copy constructor of members, and copy vtpr. If a class:
1: No virtual methods or virtual base classes
2: copy constructor of all immediate base classes is free of cost
3: copy constructor of all members is free of cost
At this time, its copy constructor has no cost, which is equivalent to implementing it with memcpy.
Determine whether it is a deep copy or a small copy, or whether it needs to be implemented based on the class. For example, if it has an object reference that is implemented using a native pointer or an object reference that is implemented using a boost: shared_ptr that references the smart pointer that shares all permissions, this copy is a shortest copy; if copy_ptr is used to implement object reference for deep copy smart pointer implementation, it is a deep copy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.