C + + Learning record: operator = reload

Source: Internet
Author: User

"Effective C + +" is indeed a book of God, the sense of reading can be described in a hearty, it seems that a lot of small details previously did not care to become enlightened, in clause 10, Scott recommends that we overload the operator ' = ' When we return a *this reference.
Assignment, we can often write chained forms:
  

int x,y,z;x = y = z = 10;

The assignment uses the right binding law, so the above chain assignment is parsed as:

x = (y = (z = 10));

First, 15 is assigned to Z, and then Z is updated to assign the value to x after the y,y update;
So in order to implement "chained assignment", the assignment operator must return a reference to the left argument of the operator.

class A{public:...    A& operator=(A& rhs)    {        ...        return *this;    }...};

C + + Learning record: operator = reload

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.