Assignment operator overload Why do I need to return a reference

Source: Internet
Author: User

First, the C + + assignment operator is intended as "reference to return the left value" (left value: The variable to the left of the assignment number, not its value)

Cases:

int A, b = 3, c = 2;
(A = b) = C;
cout<<a<<endl;

For a = B (when A,b is an object), a temporary object is generated if the reference to the left value is not returned, and the value of the last object A is the value of object B, not C. If you do not process an expression such as a = B = c, it will be normal (just call the copy constructor and the destructor to handle the temporary object).

Second, in order to carry out continuous assignment, that is, x = y = Z

1, Assignment return reference

x = y = Z executes y = Z First, returns a reference to Y, executes x = y

2, assignment does not return the reference

x = y = Z executes y = Z First, returns a temporary object initialized with Y (note that the temporary object is a constant object), and then executes the temporary object of x = y (requiring operator= (const x&)) to return a temporary object initialized with X (this requires the copy constructor to be x (const x&)).

Therefore, it is not necessary to return references, and the benefits of returning references can be known to the original semantics of the assignment and avoid the invocation of copy constructors and destructors.

"Digression": If there is no copy constructor and assignment operator in the class that describes itself, the compiler will provide it, but they all just make a shallow copy of the object. In classes that point to the heap space pointer as a data member, you must avoid using a shallow copy, and define your own assignment operator for the class to allocate heap memory to the object.

PS: Shallow copy: Invokes the system default copy constructor, no longer allocating resource memory. Deep copy: Calls its own copy constructor, allocating new resource memory.

The copy constructor creates an identical new object with an existing object. The assignment operator assigns an existing object to a similar object that already exists.

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.