Effective C + + terms 10-12 (operator= (overloaded return type, self-assignment, and deep copy) collation

Source: Internet
Author: User

First, overload operator= return type
The following example shows that operator= is similar.
For: Ostream & operator << (ostream & OS, const ClassType &object)

Explain the points:
1. The first parameter is a reference to the Ostream object on which output is generated and ostream is non-const, because writing to the stream alters the state of the stream; The parameter is a reference because the Ostream object cannot be copied (in C + + The standard input and output stream classes defined in IStream and Ostream, where the copy constructor and assignment operator functions are placed in the private section and are only declared, not defined.
2. The second parameter should generally be a reference to the class type to be output, which is a reference to avoid copying the arguments, reducing the copy, and it is set to const because the output will not normally change the object, and the const object can be used to output both const and non-const objects.
3. The return type is a ostream reference, and its value is usually the Ostream object operated by the output operator, first because the Ostream object cannot be copied, so it must be a reference, the second reference can be copied less once, improve efficiency, and finally, in order to demonstrate continuity, to achieve continuous output, The effect of manipulating a Ostream object with multiple output operators, if not a reference, generates a new temporary object when the program returns, that is, the successive two << operators are actually for different objects, which is like cout<<a<< b; the difference from cout<<a;cout<<b;.
PS: Overloaded assignment operator, continuous assignment can not return a reference
Overloaded addition operator, continuous addition cannot return a reference

Second, solve operator= self-assignment problem
It is necessary to consider self-assignment when implementing operator=-like x=y, we do not know whether the value of the variable x and Y represents the same value (it is more appropriate to say X and y as a pointer). As follows

Class Bitmap{};class Widget{public:    widget& operator= (const widget& portal);p rivate:    bitmap *pb;// Defines a pointer to the allocated space}
First-edition assignment function:
widget& widget::operator= (const widget& RHS) {    delete pb;    PB = new Bitmap (*RHS.PB);    return this;}
Such a function of PB before use to clean up before the PB point, in the acceptance of a new object, it is very logical to see, but when this and the function of the parameter RHS equal to PB = new Bitmap (*RHS.PB); it will perform an error, because we have *RHS.PB Delete it.
Second Edition assignment function:
widget& widget::operator= (const widget& RHS) {    if (*this = = RHS)          return *this;    Delete PB;    PB = new Bitmap (*RHS.PB)    return *this;}
This version of the assignment function is basically acceptable, but is not safe, because PB remains an indeterminate pointer when new creates an exception.
A third version:
widget& widget::operator= (const widget& portal) {    bitmap *porig = PB;    PB = new Bitmap (*RHN.PB);    Delete Porig;    return this;}
This function at the beginning with Porig record PB, when new no exception we are in the PB original point to the space released, thereby improving the security.
There is another idea for implementing the assignment function, which is the copy and swap technique
Class Widget {void swap (const widget& RHS);} widget& widget::operator= (const widget& RHS)    {    Widget temp (RHS);  Prevent changes to RHS    Swap (temp);    return *this;}
Of course we can also pass parameters by value
widget& widget::operator= (const Widget RHS)//pass-by-value is a copy   {    swap (temp) of the argument;    return *this;}
The disadvantage of the copy and swap technique is that the clever use of swap loses the clarity of the code, but moving the copy action to the construction stage of the function parameter makes the compiler sometimes generate efficient code.

Third, operator= overloaded functions, copy constructors, and constructors deep replication

It's easy to understand that when you implement the above function in a subclass, don't forget to call the corresponding function of the base class to copy, assign, and initialize the inherited part of the variable!!

Effective C + + terms 10-12 (operator= (overloaded return type, self-assignment, and deep copy) collation

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.