Detailed description of C ++ shallow copy

Source: Internet
Author: User

C ++ programming language is a very powerful computer application language, which not only supports the functions used in the C language, but also supports many programming styles. Here we will introduce in detail the application methods of C ++ shallow copy.

  • C ++ memory allocation skills
  • Correct understanding of the C ++ cpuid command
  • C ++ experiences in obtaining CPU Information
  • Analysis of Different Methods of C ++ Memory Management
  • C ++ iterator

C ++ shallow copy is a one-to-one value assignment between member data: assign values to the values to be copied one by one. 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 C ++. 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.

It is required regardless of the depth. When a deep copy occurs, it usually indicates that there is an "aggregation relationship", while when a C ++ 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 C ++ 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.

Before learning this chapter, we have learned about class constructor and destructor. For common type objects, copying between them is very simple, for example:

 
 
  1. int a = 10; int b =a; 

In C ++, the objects of classes defined by myself are also objects, and no one can block us from copying them in the following ways, for example:

 
 
  1. #include <iostream> 
  2. usingnamespacestd;   
  3. classTest   
  4. {   
  5. public: Test(inttemp)   
  6. {   
  7. p1=temp;   
  8. }  
  9. protected: intp1;   
  10. };   
  11. voidmain()   
  12. {   
  13. Test a(99);   
  14. Test b=a;   

Common objects and class objects are the same objects. Their features are similar and different. class objects have member variables, but common objects do not, when the same replication method occurs on different objects, the system also performs different operations on them. For Class objects, class objects of the same type are copied through the copy constructor to complete the entire replication process. In the above Code, we did not see the copy constructor, and we also completed the replication, why? Because when a class does not have a custom copy constructor, the system automatically provides a default copy constructor to complete the copy.

Next, we define a copy constructor that is the same as the default copy constructor of the system to illustrate the situation (for example, the above Code, let's see how it works internally!

The Code is as follows:

 
 
  1. # Include <iostream>
  2. Usingnamespacestd;
  3. ClassTest {
  4. Public: Test (inttemp)
  5. {P1 = temp ;}
  6. Test (Test & c_t)
  7. // Here is the custom copy constructor.
  8. {
  9. Cout <"Enter copy constructor" <p1p1 = c_t.p1;
  10. // If this statement is removed, the copy cannot be completed. The core statement of this statement is copied}
  11. Public: intp1 ;};
  12. Voidmain () {Test a (99); Test B =;
  13. Cout <cin. get ();
  14. }

Test (Test & c_t) in the code above is our custom copy constructor. The name of the copy constructor must be consistent with the class name, A function form parameter is a reference variable of this type and must be a reference.

When an initialized custom class object is used to initialize another newly constructed object, the copy constructor is automatically called, if you do not have a custom copy constructor, the system will provide a default copy constructor to complete this process. The core statement for copying the code above is to use Test (Test & c_t) copy the p1 = c_t.p1 In the constructor; the statement is complete. If this code is removed, the p1 attribute of object B will obtain an unknown random value.

The above is a brief introduction to C ++.

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.