Effective C + + reading notes clause 11

Source: Internet
Author: User

Article 11: Handling "self-assignment" in operator=

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

First edition:

#include <iostream>using namespace Std;class bitmap{public:bitmap () {cout<< "Call Bitmap () parameterless constructor" << Endl;} Bitmap (const bitmap& BT) {this->i = bt.i;cout<< "Call bitmap () copy constructor" <<endl;} ~bitmap () {}private:int i;}; Class Widget{public:widget () {PB = new bitmap ();} Widget (const widget& wd) {THIS->PB = WD.PB;} public:widget& operator= (const widget& RHS);p rivate:bitmap* PB; Defines an object allocated from the heap};//First Edition assignment function:widget& widget::operator= (const widget& RHS) {Delete PB;PB = new Bitmap (*RHS.PB) ; return *this;} This version of the function of PB before use to clean up before the PB point, and then accept a new object, it is very logical to see, but//when this and the function parameter RHS equal, PB = new Bitmap (*RHS.PB); because we've put *RHS.PB The delete dropped. int main () {Widget W1; Widget w2;w1 = W2;return 0;} /* Call bitmap () no parameter constructor call bitmap () parameterless constructor call bitmap () copy constructor Press any key to continue*/


Second Edition:

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 line, the assignment function is basically acceptable, but not necessarily safe, because when new generates an exception, PB is still a//indeterminate pointer.


Third edition:

The third edition of the assignment function:widget& widget::operator= (const widget& RHS) {Bitmap *porig = PB;PB = new Bitmap (*RHS.PB);d elete Porig; return *this;} The third version of the function at the beginning of the use of Porig record PB, when new no exception when we put PB original point to the space released//thereby improving security. This method is also able to deal with self-assignment, if rhs=*this here, we first made a backup of the original//bitmap, delete the original bitmap, point to our backup, perhaps this process of self-assignment//is not very good, but the line of the pass, It's good to use this approach in case of security.


Fourth edition:

#include <iostream>using namespace Std;class bitmap{public:bitmap () {cout<< "Call Bitmap () parameterless constructor" << Endl;} Bitmap (const bitmap& BT) {this->i = bt.i;cout<< "Call bitmap () copy constructor" <<endl;} ~bitmap () {}private:int i;}; Class Widget{public:widget () {cout<< "Call Widget () parameterless constructor" &LT;&LT;ENDL;PB = new Bitmap ();} Widget (const widget& wd) {cout<< "Call widget () copy parameter constructor" &LT;&LT;ENDL;THIS-&GT;PB = WD.PB;} void My_swap (widget& rhs);p ublic:widget& operator= (const widget& RHS);p rivate:bitmap* PB; Defines an object allocated from the heap};//the fourth edition assignment function: void Widget::my_swap (widget& rhs) {PB = RHS.PB;} widget& widget::operator= (const widget& RHS) {Widget temp (RHS);//Prevent Change Rhsmy_swap (temp); return*this;} The fourth edition of the assignment function uses the copy and Swap technique, which is explained in clause 29//and is not carefully read. Fourth edition can also use:widget& widget::operator= (Widget rhs) {My_swap (RHS),//is essentially the same, because the passed parameter is a value pass, so here is a copy of the RHS, Equivalent to widget temp (RHS), mainly to prevent RHS from being changed; return*this;} int main () {Widget W1; Widget w2;w1 = W2;return 0;} /* Call Widget () no parameter constructor call bitMap () No parameter constructor call widget () parameterless constructor call bitmap () parameterless constructor call widget () copy parameter constructor Press any key to continue*/ 


Summarize:

1: Make sure that operator= has good behavior when the object is self-assigning, where techniques include comparing addresses of "original objects" and "target objects." , thoughtful sequence of statements, and copy and swap

2: Determine if any function operates on more than one object, and when multiple objects are the same object, its behavior is still correct.

Effective C + + reading notes clause 11

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.