C + + Learning Notes Object-oriented advanced programming @boolan

Source: Internet
Author: User

function parameter & return value (reference value efficiency is higher than value pass)

1) Consider using object references as much as possible, and use const for situations where the function does not change the content of the object.

2) Return value use object reference as much as possible, only return object cannot be a reference if the return value is a local object

The reason is that the local object will be refactored after the function has finished executing, and the external reference will correspond to an invalid object.

For a chestnut to understand the function of the argument, assuming that you have a file to be handled by others, you have to pass the file to others in a way that simply can have two kinds:

1, a copy of the document sent to each other, then let him deal with, such a benefit is that his changes will not affect your original file

The disadvantage is that space is wasted, which is equivalent to the way the function passes the parameter values.

2, tell the other side you want to deal with the location of the file, let him visit and deal with, such a benefit is to save space time,

The bad thing is that the other person will change to your original file, in order to deal with this situation, you can consider setting the appropriate permissions, such as read-only

This method is equivalent to the reference pass of the function parameter, and the setting of the permission is whether to add const

Second, operator overloading

Format: return value opreator operator (argument list) {function Body}

1) Note the parameter and return value of the function

2) for cases in which operators need to be used continuously, return a reference

Third, the use of const

1) Use a const reference for immutable object arguments

2) using the const identifier for member functions that do not modify member data is a good coding habit

C + + Learning Notes Object-oriented advanced programming @boolan

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.