Why does the C + + assignment function return reference to *this?

Source: Internet
Author: User

Why do assignment operations return reference to *this? Before you figure this out, understand the return value type of the function: return value type, return reference type
    • Return value type: Returns a copy of an object.
  
 
  1. test operator= (const test &t)
  2. {
  3. ...
  4. cout << "赋值" << endl;
  5. return *this;
  6. }
When an assignment between test objects appears in the program, call the operator function. When return *this, the copy construct test (const TEST&T) is executed, at which point the entry is *this (that is, the object that invokes the Operator= function), and the returned object is constructed from *this.
    • Returns the reference type: The object itself is returned.
  
 
  1. test &operator= (const test &t)
  2. {
  3. ...
  4. cout << "赋值" << endl;
  5. return *this;
  6. }
As in the above, when an assignment between test objects appears in the program, the operator= operation is performed, and when return *this, the return is the call to the assignment action object itself.
Effective C + + said, is to achieve chain assignment such as a = B =c; The next example, test, if you do not return a reference, you can not achieve the chain assignment?
  
 
  1. class test
  2. {
  3. public:
  4. test() :i(10)
  5. {
  6. cout << "构造" << endl;
  7. }
  8. ~test()
  9. {
  10. }
  11. test(const test &t)
  12. {
  13. this->i = t.i;
  14. cout << "拷贝" << endl;
  15. }
  16. test operator= (const test &t)
  17. {
  18. i = t.i;
  19. cout << "赋值" << endl;
  20. return *this;
  21. }
  22. void setData(int value)
  23. {
  24. i = value;
  25. }
  26. int getData()
  27. {
  28. return i;
  29. }
  30. private:
  31. int i;
  32. };
  33. int _tmain(int argc, _TCHAR* argv[])
  34. {
  35. test a;
  36. test b;
  37. test c;
  38. cout << a.getData() << endl << b.getData() << endl<<c.getData() << endl;
  39. a.setData(20);
  40. c = b= a;
  41. cout << a getdata () << Endl << b getdata () << Endl << c getdata () << Endl ;
  42. return 0;
  43. }
The result: it can be seen that only one copy is made after each assignment. Now consider a situation
If it is (c=b) =a, how about this? Results:. Analysis: Because C=b, the return is a temporary object, so in fact finally a assignment gave a temporary variable and a copy once more. If you replace reference to *this,test&operator= (ConstTest&T),View Results (c=b) =a,Object C was successfully assigned, and the entire process was copied two times less.

Some children's shoes may have to ask, why return this, not the other. First, make it clear that this is a pointer to the object. A=b, the assignment constructor for object A is called, and after the assignment is finished, it is sure to return object A. For example int a = 1; After the assignment is finished, a is definitely returned.
Summary: The assignment constructor, in order to implement the assignment operation, can accept the assignment of other objects as an lvalue. Therefore, a reference to *this must be returned, and if the value type is returned, and if it is an lvalue, its object is not given the desired assignment result because it is a temporary object of the value type as an lvalue. When the reference reference to *this is returned, the assignment operation as an lvalue gives the correct assignment result.







From for notes (Wiz)

Why does the C + + assignment function return reference to *this?

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.