Deep copy and shallow copy in C + + deep copy in qt, shallow copy and implicit sharing

Source: Internet
Author: User
Tags shallow copy

  • The following is a deep, shallow copy defined in C + +
    • When you initialize another newly constructed object with a custom class type Object that has already been initialized, the copy constructor is automatically called. In other words, the copy constructor is called when the object of the class needs to be Copied. the copy constructor is called in the following cases:
      (1) An object is passed into the function body in the way of value passing
      (2) An object is returned from the function in the way that the value is passed
      (3) an object needs to be initialized by another object.

      If a copy constructor is not explicitly declared in the class, the compiler will automatically generate a default copy constructor that completes the bit copy between the Objects. A bit copy is also called a shallow copy, which is explained Later.

      Custom copy constructors are a good programming style that prevents the compiler from forming a default copy constructor, which improves the efficiency of the source Code.

      Shallow copy and deep copy

      In some cases, In-class member variables need to dynamically open up heap memory, If a bit copy is implemented, that is, the value of the object is completely copied to another object, such as A=b. At this point, if a member variable pointer in B has already applied for memory, that member variable in a also points to the same piece of memory. There is a problem: when B releases the memory (for example, a destructor), A pointer inside a is a wild pointer, and a run error Occurs.

      Deep copy and shallow copy can be simply understood As: if a class has resources, when the object of this class has a replication process, the resource is redistributed, the process is a deep copy, conversely, no redistribution of resources, is a shallow copy. Here is an example of a deep copy.


    • Summary: there is a reallocation of resources: deep copy; none: Shallow copy (directly calling the default constructor is actually a memory that shares a piece of data, and two objects point to this data Memory)
  • Here is a copy of the depth of qt, I think it's something more practical to understand.
    •   

      1. Shallow copy:

      Shallow copies, like reference types,

      A shallow copy is a source object that is shared with a copy object, and only refers to a variable (with a different name). Changes to any of these objects affect the other Object. For example, a person at the beginning of the call Zhang San, later renamed John doe, but still the same person, whether it is Zhang three missing arms or legs or Li four short legs, is this person Unlucky.

      2. Deep copy:

      Deep copies, for example, are value Types. Changes the memory point of the data, and the memory allocation Changes.

      The value object, such as the predefined type Int32,Double, and struct (struct), enumeration (enum), andso On.

      3. Implicit sharing:

      Implicit sharing is also called Write-back Replication. When two objects share the same data (through a shallow copy to achieve the sharing of data blocks), if the data does not change, the data is not Copied. A deep copy is executed when an object needs to change the Data.

    • Instance:
      voidmainwindow::on_pushbutton_8_clicked () {QString str1="Data"; Qdebug ()<<"String addr ="<< &AMP;STR1 <<", "<<Str1.constdata (); QString str2=str1;//Shallow copy points to the same data blockQdebug () <<"String addr ="<< &AMP;STR2 <<", "<<Str2.constdata (); str2[3]='e';//a deep copy, the Str2 object points to a new data structure that is different from what str1 points toQdebug () <<"String addr ="<< &AMP;STR2 <<", "<<Str2.constdata (); str2[0]='F';//does not cause any form of copying because the data structure that str2 points to is not sharedQdebug () <<"String addr ="<< &AMP;STR2 <<", "<<Str2.constdata (); STR1=str2;//the data structure that str1 points to will be freed from memory, and the Str1 object points to the data structure that str2 points toQdebug () <<"String addr ="<< &AMP;STR1 <<", "<<Str1.constdata (); Qdebug ()<<"String addr ="<< &AMP;STR2 <<", "<<Str2.constdata ();}

      The results of the measured output are as follows (my analysis in parentheses):

      String addr = 0x28c798, 0x14316660 (str2 pointer address, pointing to the previous qshareddatapointer, in fact, data1)
      String addr = 0x28c798, 0x1433f2a0 (str2 pointer address, pointing to a new qshareddatapointer, named Data2)
      String addr = 0x28c798, 0x1433f2a0 (str2 pointer address, pointing to data2, but modifying its contents)
      String addr = 0x28c79c, 0x1433f2a0 (str1 pointer address, points to data2, does not modify its contents, and discards data1 so that the reference count is zero and is completely released)
      String addr = 0x28c798, 0x1433f2a0 (str2 pointer address, pointing to data2, not modifying its contents)

      Note that the address of str1 and the Str1.constdata () address are not the same thing.

Deep copy and shallow copy in C + + deep copy in qt, shallow copy and implicit sharing

Related Article

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.