About C + + return * This

Source: Internet
Author: User

Turn from: 45222905

return * this

#include <iostream>using namespacestd;classtest{ Public:    //constructor FunctionTest (intI=0,Doublej=0.0): M_i (i), M_j (j) {}//Assignment Constructorstest&operator= (Test &RHS) {         This->m_i =rhs.m_i;  This->m_j =Rhs.m_j; return* This; }        intm_i; DoubleM_j;}; intMain () {//1, assign the initial value, Output 1 2.3Test Obj1 (1,2.3); cout<< obj1.m_i << Endl << obj1.m_j <<Endl; cout<< Endl <<"-------------------"<<Endl; //2, the initial value is not assigned, output 0 0Test Obj2; cout<< obj2.m_i << Endl << obj2.m_j <<Endl; cout<< Endl <<"-------------------"<<Endl; //3, the initial value is not assigned, but the assignment constructor is assigned to the initial value , but attention is repeated call assignment constructor assigns the initial value. Test obj3; Obj3= Obj2 = Obj1;//here, the advanced obj2 = Obj1 operation, (after the assignment succeeds through return *this, returns the Obj2 object), and then the operation obj3 = Obj2. cout<< obj2.m_i << Endl << obj2.m_j <<Endl; cout<< Endl <<"-------------------"<<Endl; /** * * Through Example 3, you will find that if there is no return * This, you can not go back to the Obj2 object, then in the obj3=obj2 operation, the assignment constructor will not be used.    C + + to make it easier for us to use an assignment like int i=j=k=5, you must return the object each time an assignment is performed in a right-to-left assignment, and then proceed to the left. *****/     return 0;}

There is another good explanation: 22220777

About C + + return * This

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.