C + + about functions by value transfer and by address pass __ function

Source: Internet
Author: User

In-address delivery, the reference parameter is a reference to the argument variable, as mentioned before, the reference is just the equivalent of an alias, the system will not allocate storage space for it, so at this time, the function changes the parameters, the actual parameter values will change accordingly. Passing by value simply passes the value of the argument to the parameter variable, participates in the operation, because it is in a different storage unit, so the argument value is not affected.

Value passing (passed by value) 1. The argument value is passed to the corresponding parameter 2. The argument address is passed to the corresponding parameter, such as an array, a pointer.

Address delivery (reference passing) uses aliases, where the shared storage space (direct access) parameter is referred to as a reference, and the argument is generally a variable.

For example:

int func (int *p,int *q) {}

void Main ()

{

int a,b func (&a,&b);

}

In the main function, the storage space of the variable a,b is opened up, when the Func function is executed, the system will open up the storage space needed for the p,q, except that P and Q point to A,b respectively. Therefore, the argument is passed the address value to the formal parameter, or it is passed by value. If the program changes to this (reference call):

int func (int &p,int &q) {}

void Main ()

{

int a,b func (a,b);

}

In main function, the storage space of variable a,b is opened up, when the Func function is executed, the system does not open up storage space for p,q, but uses the storage space of real parameter a,b. Therefore, the combination of the actual situation, the formal parameter part does not open up storage space.

The value of the function's parameter itself is unchanged before and after the call, and any modifications made within the function body have no effect on the value outside the function. Even if the pointer is passed, in essence, C + + also made a copy of the pointer to a copy of the pass to the function.

In the case of passing the pointer, the modified is not the value of the pointer parameter, but the value of the space to which the pointer parameter is pointing. The first question: pointer pass: belong to pass by 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.