C++ copy constructor of of deep copy and shallow copy details

  • 2020-04-02 01:29:22
  • OfStack

For objects of ordinary types, it is easy to copy between them, for example:
Int a = 88;
Int b = a;
But the class object is different from the ordinary object, the internal structure of the class object is generally more complex, there are various member variables. Let's look at a simple example of copying a class object.


#include <iostream>
using namespace std;
class CExample {
private:
     int a;
public:
     CExample(int b)
     { a=b;}
     void Show ()
     {
        cout<<a<<endl;
    }
};
int main()
{
     CExample A(100);
     CExample B=A;
     B.Show ();
     return 0;
} 

Run the program, screen output 100. As can be seen from the running results of the above code, the system allocates memory for object B and completes the replication process with object A. In the case of class objects, class objects of the same type are copied through a copy constructor. The following example illustrates how the copy constructor works.

#include <iostream>
using namespace std;
class CExample {
private:
    int a;
public:
    CExample(int b)
    { a=b;}

    CExample(const CExample& C)
    {
        a=C.a;
    }
    void Show ()
    {
        cout<<a<<endl;
    }
};
int main()
{
    CExample A(100);
    CExample B=A;
    B.Show ();
    return 0;
}  

CExample(const CExample& C) is our custom copy constructor. As you can see, the copy constructor is a special constructor whose name must be the same as the class name, and whose only argument is a reference variable of this type, which is of const type and cannot be changed.
For example, the copy constructor of class X takes the form X(X& X).

The copy constructor is called automatically when an initialized custom class type object is used to initialize another newly constructed object. That is, the copy constructor will be called when the class object needs to be copied. The copy constructor is called when:
An object is passed into the body of a function as a value
An object is returned from a function as a value pass
An object needs to be initialized by another object.

If a copy constructor is not explicitly declared in the class, the compiler automatically generates a default copy constructor that does the bit-copying between objects. Bit copy is also known as shallow copy, which will be explained later.

Custom copy constructors are a good programming style that prevents the compiler from forming the default copy constructor and increases source code efficiency.

Shallow and deep copies
In some cases, in-class member variables need to be dynamically allocated heap memory, and in the case of bit-copying, the value in the object is copied exactly to another object, such as A=B. At this point, if A pointer to A member variable in B has applied for memory, then that member variable in A also points to the same block of memory. This presents A problem: when B frees memory (e.g., destruct), the pointer inside A becomes A wild pointer, and an error occurs.

Deep copy and shallow copy can be simply understood as: if a class owns a resource, the process of resource redistribution when the class's objects are copied is called deep copy; conversely, if there is no resource redistribution, the process is called shallow copy. Here's an example of a deep copy.


#include <iostream>
using namespace std;
class CA
{
 public:
  CA(int b,char* cstr)
  {
   a=b;
   str=new char[b];
   strcpy(str,cstr);
  }
  CA(const CA& C)
  {
   a=C.a;
   str=new char[a]; //Deep copy
   if(str!=0)
    strcpy(str,C.str);
  }
  void Show()
  {
   cout<<str<<endl;
  }
  ~CA()
  {
   delete str;
  }
 private:
  int a;
  char *str;
};
int main()
{
 CA A(10,"Hello!");
 CA B=A;
 B.Show();
 return 0;
} 

The definition of deep copy and shallow copy can be simply understood as: if a class has a resource (heap, or other system resource), when the object of that class has a copy process, the process can be called deep copy; conversely, the object has a resource, but the copy process does not copy the resource.

A shallow copy of a resource will cause an error in the process of releasing the resource.

Test(Test &c_t) is a custom copy constructor whose name must be the same as the class name and whose formal argument is a reference variable of this type and must be a reference.

When using a custom class has been initialized with the type of object to another new constructed object initialization, the copy constructor is automatically calls, if you don't have a custom copy constructor, the system will provide a default copy constructor to complete this process, copy the core code of the above statement is through the Test, the Test & c_t) p1 = c_t within the copy constructor. The p1. Statement complete.


Related articles: