What happens when Objective C ++ (11) assigns a value to itself (a =?

Source: Internet
Author: User

Problem focus:

Self-assignment seems a bit silly. In fact, it always happens first: it is legal, and second, it is not necessarily safe. Again, it is sometimes not so obvious.
First look at a Demo
Class Widget {...}; widget w ;... /** the most obvious self-assignment **/w = w; /** less obvious self-assignment ** // a [I] = a [j]/** potential that achieves I = j or something with the same effect in a certain place */* px = * py; /** more concealed self-assignment "alias" **/class Base {...}; class Derived: public Base {...}; void doSomething (const Base & rb, Derived * pd); // The rb and * pd may be the same object.

Generally, if some code operation pointers and music can be used to point to multiple objects of the same type, you need to consider whether these objects are the same.
Two potential risks of Self-assignment are as follows: Self-assignment security exceptions:
Class Bitmap {...}; class Widget {... private: Bitmap * pd;};/** operator = code implementation **/Widget & Widget: operator = (const Widget & rhs) {delete pb; pb = new Bitmap (* rhs. pb); return * this ;}
Self-assigned security:
If rhs is the same as the current object, The rhs object is also destroyed when the pb of the current object is destroyed.

Improvement: identity test
/** Operator = code implementation **/Widget & Widget: operator = (const Widget & rhs) {if (this = & rhs) return * this; // identify the test delete pb; pb = new Bitmap (* rhs. pb); return * this ;}

The improved code above avoids the security issue of Self-assignment. Let's take a look at the second problem: abnormal security:
If a new Bitmap () exception occurs, the result is that the pb value is straightforward, and the Widget finally points to a deleted Bitmap.
Improvement 1: dedicated statements
/** Operator = code implementation **/Widget & Widget: operator = (const Widget & rhs) {Bitmap * pOrig = pb; pb = new Bitmap (* rhs. pb); // if the new Bitmap throws an exception, the pb will remain the original delete pOrig; return * this ;}

Solution 2: copy and swap
class Widget {...void swap(Widget& rhs);...};Widget& Widget::operator=(const Wdiget& rhs){    Widget temp(rhs);    swap(temp);    return *this;}
Thoughts:
The copy constructor must be declared as "pass by value". Passing by value will result in a copy.


Summary:
Make sure that operator = has good behavior when the object is assigned a value.
Including:
Object Source
Target object address
Statement order
Determine if any function operates on more than one object, and multiple objects are the same object, the behavior is still correct.



References: Objective C ++ 3rd

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.