Summary of copy constructors in C++

  • 2020-04-02 01:37:58
  • OfStack

1. What is a copy constructor:
Copy constructors, of course, are just copy and construct. Copy is also called copy, so copy constructor is also called copy constructor. Copy constructor, is a special constructor, it is called by the compiler to complete some other objects based on the same class of construction and initialization. Its unique parameter (a reference to the object) is immutable (const type). This function is often used to pass and return values of user-defined types when a function is called.

2. Copy the constructor form


Class X
{
public:
  X();
  X(const X&);//Copy constructor
}

2.1 why are copy construction parameters of reference type?
Here's why: when an object passes a function by passing a value, the copy constructor is automatically called to generate the object in the function (in the case of the copy constructor call). If an object is passed its own copy constructor, its copy constructor will be called to copy the object so that the copy can pass its own copy constructor, which will cause an infinite loop to Stack Overflow.

3. Copy the three forms of constructor calls
3.1. An object is passed into the body of a function as a parameter.
3.2. An object returns a value as a function, passed in the form of a value.
3.3. An object is used to initialize another object (often referred to as a copy initialization).

Summary: when an object is passed by value (either as a function argument or as a function return value), the compiler creates a temporary copy of the object, and the copy constructor of the class is called when the temporary copy is created.

4. Deep and shallow copies
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 copies are also known as shallow copies, as described later.) Custom copy constructors are a good programming style that prevents the compiler from forming the default copy constructor and increases source code efficiency.

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. Actually, this is where the deep copy comes in, so you have to customize the copy constructor.

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;
}

A shallow copy of a resource will cause an error in the process of releasing the resource. It is important to note that there are no pointer members in the class.

5. Copy the constructor and the "=" assignment operator
Such as:

class CExample
{} ; 
int main()
{
CExample e1 = new CExample;
CExample e2 = e1;//Call the copy constructor
CExample e3(e1);//Call the copy constructor
CExample e4;
e4 = e1;//Call the = assignment operator
}

The general principle is: (1) for any class containing dynamically assigned members or containing pointer members should provide a copy constructor; (2) while providing a copy constructor, you should also consider overloading the "=" assignment operator symbol.


Related articles: