The return value of C + + is the case of the reference Reference---supplement (6) "Effective C + +" __c++

Source: Internet
Author: User

In the last blog we talked about returning a reference object may be wrong or inefficient, there is no more appropriate to return the reference reference. Let's summarize the types below:
1 function return value with reference, parameter pass in also use reference:

int& Hel (int& t) {return
    t;
}

2 function return value with reference, parameter pass with pointer

int& Hel (int *t) {return
    *t;
}

3 returns the data inside the function: Copy assignment operator

Class a{public
:
    A (int xx): x (XX) {
    }
    A () {

    }
    int GetX () const{return
        x;
    };
    void Show () {
        cout << x << endl;
    }
    A (const a&a) {
        this->x = a.x;
    }
    a& operator= (const a& A) {
        this->x = a.x;
        return *this;
    }
Private:
    int x;
};
Internal data
Class point{public
: Point
    (int x,int y);
    ...
    void SetX (int newval);
    void sety (int newval);
    ...
};
struct rectdata{point
    ulhc;
    Point lrhc;
};
Class rectangle{public
:
    ...
    point& upperleft () const 
    {return
    pdata->ulhc;
    }
    point& lowerright () const{return
    pdata->lrhc;
    }
Private:
    Rectangle pData;

The above two situations are more suitable for returning references, which is more efficient than the return object.
Reference Blog: http://www.cnblogs.com/lionfight/archive/2012/07/22/2603865.html

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.