"C + +" C + + reference return value to be cautious

Source: Internet
Author: User

Source Address Http://onlywish.me

The reference is not as complex as a pointer, and does not copy space, and always prefers it.

       then delve deeper into C + + reference issues        don't know how the use of reference return values is a big risk        Foundation:               when an object is passed as a value. The copy constructor of the class is called, Common, function parameters. = number assignment.        I carried out the following experiment:             has a function defined as follows:                        A & func ();             returns a reference to a class           the original, which I have called. A = func (); I believe that's what most people call          after an experiment, the copy constructor is called once          actually returns a reference to the stack, and then the variable a duplicates the object inside the stack        so, when the reference function is called later, To call A & a = func ();       if you want to write a class package to someone else, try to avoid referencing the return, because the wrong call will lead to the heap memoryData is not released.

"C + +" C + + reference return value should be cautious

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.