1. What should the overloaded assignment operation function in C + + return?
The class overload assignment operator is generally present as a member function, what type should the function return? Refer to the built-in type assignment operations, such as
int x, y, Z;
x=y=z=15;
The assignment behavior is equivalent to x= (y= (z=15)), which means that the assignment operation should return a reference to the left operand, so in order to be compatible with the built-in type, the overloaded assignment operator in the class should return a reference to the left operand, that is, the declaration of the overloaded assignment operation function of the *this Class A,
Class a{};
a& a::operator= (const a&);
2. Ensure that overloaded assignment operations have good behavior
For an assignment operation, the first thing you should think about is how to handle the self-assignment, which is especially important when the class contains data of the pointer type, as shown below
Class mystring{
Public
...
MyString (char *p=null);
mystring& operator= (const mystring&);
Private
Char *str;
};
MyString A ("Hello");
MyString B ("World");
B=b;
We know that when assigning a value, you first release the left operand's resource, then assign the left operand to the right operand, and the assignment action function is as follows
mystring& mystring::operator= (const mystring& RHS) {
delete [] str;
Str=new Char[strlen (RHS.STR) +1];
strcpy (STR,RHS.STR);
return *this;
}
When running a=a, there will be unintended consequences, we first release the left operand in the function of the resource, and then access the right operand of the resource, and then when the self-assignment occurs, THIS==&RHS, that is, we first freed the resources, and then access the content of the freed resources, This obviously causes the program to crash, so we need to perform validation of the self-assignment operation, and the modified function is as follows
mystring& mystring::operator= (const mystring& RHS) {
if (THIS==&RHS)
return *this;
delete [] str;
Str=new Char[strlen (RHS.STR) +1];
strcpy (STR,RHS.STR);
return *this;
}
At this point, the function handles the self-assignment, but there is still a problem, what if the new operation fails?
We have freed the left-hand operand, but failed to allocate resources due to lack of space, and if we continue to access the freed resources, there will be undefined results, the assignment operation will not have the exception security, there are two solutions for this problem
First, adjust the order of the statements, save the original resources, etc., and then release the previous resources after the reallocation of resources
mystring& mystring::operator= (const mystring& RHS) {
Char *ptemp=str;
Str=new Char[strlen (RHS.STR) +1];
strcpy (STR,RHS.STR);
delete [] ptemp;
return *this;
}
This code can also handle self-assignment, but it must perform all the copy, allocation, and release operations in the function, and if the probability of self-assignment is higher, we can also put the test statement into the function.
Second, the use of copy and swap technology
mystring& mystring::operator= (const mystring& RHS) {
if (THIS!=&RHS) {
MyString temp (RHS);
Char *ptemp=str;
STR=TEMP.STR;
Temp.str=ptemp;
}
return *this;
}
This technique assigns pointers instead of reassigning resources to objects, and we re-construct a new temporary object within the IF statement, then swap the str of the temporary object with the str of this class object, and when the program executes outside the IF statement, the temporary object automatically calls the destructor, frees its own resources, The resource held by the temporary object is the resource held by the original this, which is released, and now the resource held by this is the resource held by RHS, that is, the same space that STR in RHS and this is pointing to.
3. Never forget to copy every element when copying an object
For a class that has an inheritance relationship, when you write a copy-controlled function on its derived class, because the derived class cannot access the private member in the base class, in the overloaded operator function of the derived class, you need to first call the assignment operator function of its base class to assign a value to the base class member in the derived class, and then assign a value to the
C + + overloaded assignment operator