C + + Three kinds of method of transmitting parameters

Source: Internet
Author: User

There are three kinds of methods for transmitting parameters in C + +: Value passing (copy), pointer passing (address), reference passing (alias).

(1) Value passing: In fact, an assignment operation, when an argument is passed to a formal parameter, an additional copy is generated, and the parameter is just a copy of the argument, so the operation of the parameter within the function is not updated to the source argument. At the end of the function, the parameter is freed as a local variable and has no effect on the argument. If the object of the class invokes the copy construct, this deep copy operation will affect the efficiency of the transfer parameter.

(2) pointer passing: Because the address of the actual argument is passed, the pointer points to the same object in memory, so the inside of the function will "update" the parameter to the argument synchronously.

(3) Reference passing (c + + specific): The alias of an argument is passed, and when the parameter is bound to an argument object, the operation of the parameter inside the function is also "synchronously updated" to the source argument.

Note: For pointer passing and reference passing, parameter changes are a side effect of changing arguments, and we can easily modify the passed arguments by using this side effect, in addition, this avoids the copy in the value passing, and can effectively improve the efficiency for the argument of big data. Therefore, for situations where you want to increase the efficiency of the communication and do not want to affect the arguments, you can use the const:

C + + Three kinds of method of transmitting parameters

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.