C ++ destructor, copy, assignment, some knowledge of the movement of the copy function (incomplete)

As we all know, C ++ classes if there is no default constructor, automatically generates a.

Similarly, if there is no copy constructor i.e. A :: A (const A &) {} function, the system will automatically generate a, but the automatically generated copy constructor may not meet our requirements. Destructor generated automatically (if not defined).

Such as the following examples:

1 class A{
2     public:
3     int* a;
4     int b;
5     A()=default;
6     A(int x):a(new int(10)){b=x;}
7     ~A(){delete a;cout<<"我删除a了!"<<endl;}
8 };

 

Which we define a default constructor, another overloaded version of the constructor. But we do not define the copy constructor, so the system automatically help us generate a role can be roughly understood as the following function:

A(const A& another){
    a=another.a;
    b=another.b;
}

We have not defined destructor, the system automatically generates a similar to the following:

~ A(){
  delete a;
  delete b;
}

And we note that there is a class A pointer member, it will simply copies the pointer to another variable in the A default copy constructor. The A x1 (x2); x1 and

x2 is a pointer members is the same. This is not our intention, our intention is in the code above, line 5: A member of each of a variable should be initialized to address a new int variables, and between the different members of a A variable should be different.

So the question that may arise is: If space is freed x2, x1 is a member also invalid, pointing to its value is undefined. .

Or there may be another function so defined, a better understanding of:

void f(A temp)
{ 
      //.......
}

Then call f (x1), when will first call the copy constructor, a copy of a copy of x1 as a parameter. Then we have this copy temp and as a member of a x1, when the exit function f, a member of a temp is released destructor, which leads to x1 of a member has become a field guide.

 

Our own definition of a good correct copy constructor to solve the above problems.

 

Therefore, members of the class encountered when non-conventional type (such as pointers), it must write their own copy constructor, assignment operator overloading, constructors mobile, mobile assignment operator overloading, destructor.

Note: If only the mobile overloaded constructor or move assignment operator, the compiler will not automatically help you generate copy constructor and assignment operator overloading, but will default definition for deletion (= delete;).

 


 

The following look at various constructor and copy functions, deepening the next impression.

Note that if we have A x1;

A x2 = x1; and A x3; x3 = x1; A is not the same!

The former is initialized when the statement, a copy of belonging to initialize. Calling the copy constructor (  A & (const A & Another) {} )

The latter is the first statement, the default initialization. Then assignment. First calling the default constructor ( A () {} ), and then call the overloaded assignment operator, i.e. (  A & operator = (const A & A)  )

 

. 1  class A
 2  {
 . 3  Private :
 . 4      int X;
 . 5  public :
 . 6      A () {COUT << " A () " << endl;}                                                          // default constructor 
. 7      A ( int && A) X = {A; << COUT " a (int && a) " << endl;}                                        // overloaded constructor 
. 8      a (a && a) {COUT << " a (a a &&) " << endl; X = AX;                                         } // Move Copy Function
 9     A(A& a){cout<<"A(A& a)"<<endl;x=a.x;}                                           //拷贝构造函数
10     A& operator=(const A&& a){if(this!=&a){x=a.x;}cout<<"A& operator=(A&&)"<<endl;} //移动赋值符
11     A& operator=(const A& a){if(this!=&a){x=a.x;}cout<<"A& operator=(A&)"<<endl;}        ~ A () {<< COUT12 iscopy assignment operator//
" Deleted! " << endl;}                                                     // destructor 
13 is  };
 14  int main ()
 15  {
 16      
. 17      int A = . 1 ;
 18 is      A X1;
 . 19      A X2 ( . 4 );
 20 is      A X3 = Move (X2 );
 21 is      A X4 = X3;
 22 is      X1 = Move (X2);
 23 is      X2 = X3;
 24      getchar ();
 25      return  0 ;
 26 is }

 

Output: 

 

 

 


 

Another point of knowledge, as if to prove safety before the offer also see over here with, was not deep impression:

Writing a class when the assignment operator overloading , several requirements:
1. Since the assignment can operate normally not given.
2. assignment operator generally set both the copy constructor and destructor functions.

3. Do not delete the data first, and then copy the data to the new space this is! So easy to delete finished his, but a copy has failed abnormally, then the instance of the original data had no

example:

1 A& operator=(const A& x){
2         if(this!=&x){
3             A temp(x);
4             a=temp.a;
5             b=temp.b;
6             //......//
7         }
8         return *this;
9     }

Such a purpose is to avoid throwing new abnormality in the function space, it causes data changes to previous examples. The above code A temporary variables If the application fails, the function exit, the data will not affect the original instance.

First create a temporary variable , followed by the value assigned to the members of this member variable, and finally returns a reference to the current instance, temp destructor also be automatically released when this function exits. Of course, this example is based on the premise already written copy constructor and the destructor , otherwise the function programmer should write their corresponding functions.

Guess you like

Origin www.cnblogs.com/FdWzy/p/12288079.html