"Thinkinginc++" 40, on the effect of const on function return value

Source: Internet
Author: User
Whenever you pass an address to a function, you should modify it as const as you can .

/*** book: "thinkinginc++" * function: On the role of const on function return value * Time: September 7, 2014 14:14:57* Author: cutter_point*/class x{    int i;public:    X (int ii=0);    void modify ();}; x::x (int ii) {    i=ii;} void X::modify () {    i++;} X f5 () {    return x ();//An object, the default constructor, returns an lvalue that can be assigned to modify}const x f6 () {    return x ();//return the const type of}void F7 (x& X) { C10/>x.modify ();} int main () {    F5 () =x (1);    F5 (). modify ();    //! F7 (F5 ());   Here is the reference, C + + cannot be compiled by the reason that parameter passing will establish a replica    //! f6 () =x (1);  Returns a const type that cannot change the value of    //! f6 (). modify ();    //! F7 (f6 ());   Here the argument is a reference, not a const reference to    return 0;}


"Thinkinginc++" 40, on the effect of const on function return value

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.