Effective C + + terms 11,12 handling "self-assignment" in operator= | | Don't forget every ingredient when copying an object

Source: Internet
Author: User

1. Potential self-assignment

A[i] = A[j];

*PX = *py;

When two objects come from the same inheritance system, they may not even need to be declared as the same type to create aliases.

The question now is: If you point to the same object, when one of the objects is deleted and the other is deleted, this can result in unwanted results.

What should I do?

Like what:

widget& Widget:: operator+ (const widget& RHS)

{

Delete PD;

PD = new Bitmap (*RHS.PB);

return *this; Here if *this and RHS are the same object. is not well handled.

}

Solution:

1. Test with Certificate

Judging is not the same object, if it is the same object, it is self-assignment, do not do anything.

if (this = &RHS) return *this;

2, remember the original PB, is to open up a space to store its original address. We just need to remember not to remove PB before replicating the PB point.

Bitmap * Porig = PB; Remember the original PB

PB = new Bitmap (*RHS.PB); Make PB point to a copy of *PB

Delete Porig; Remove the original PB

return *this;

3. Make a copy directly (reference pass)

Widget temp (RHS); Make a copy of RHS data

Swap (temp);

4. Value passing

Swap (RHS);

Remember:

1, to ensure that the party object self-assignment, there is good behavior. The technology includes the above four kinds.

2. Make sure that if any function operates more than one object, and that more than one object is the same object, its behavior must be correct.

Remember that clause 5 mentions that the compiler will provide us with copy constructors and copy assignment functions when necessary, and they may work well, but sometimes we need to write our own copy constructors and copy assignment functions. If so, we should make sure that the "every" member is copied (copied).
If you add a member variable to a class, you must also modify the corresponding copying function (all constructors, copy constructors, and copy assignment operators).
In the constructor of a derived class, the copy constructor and the copy assignment operator should show the function that corresponds to the calling base class, or the compiler might be "smart."
When you write a copying function, make sure that:

(1) Copy all local member variables;

(2) Call the appropriate copying function within all base classes.

However, we should not make the copy assignment operator call the copy constructor or make the copy constructor call copy assignment operator. Think of it as a copy (build object) and an assignment (the object already exists).


Please remember:

    • 1. The copying function should ensure that "all member variables within the object" and "All base class members" are copied;
    • 2. Do not attempt to implement another copying function with one of the copying functions. Common functions should be put into the third function and called together by two copying functions.

Related Article

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.