Mode transfer function parameters

C / C ++ parameter passing mode has three functions, the value of transfer, transfer addresses, passed by reference.

Value transfer

Copies the value of the function parameter arguments involved in the operation inside the function value is not changed after the original argument returned.
Form as follows:

void swap(int a, int b)
{
    int temp=a;
    a=b;
    b=temp;
}
int main(void)
{
    int x=0,y=1;
    swap(x,y);
    ...
}

Delivery address

Function parameter is a pointer type, storage address arguments, parameters, arguments point to participate in the internal operation function, values of both synchronous change.
Form as follows:

void swap(int *a, int *b)
{
    int temp=*a;
    *a=*b;
    *b=temp;
}
int main(void)
{
    int x=0,y=1;
    swap(x,y);
    ...
}

Passed by reference

Note: reference semantics is not available in C, C ++ support only.
Pass a reference to the variable corresponding to the alias from participation calculate in function argument values between Form synchronization changes.
Form the following, the attention area value transfer, except only a function definition parameter forms:

void swap(int &a, int &b)
{
    int temp=a;
    a=b;
    b=temp;
}
int main(void)
{
    int x=0,y=1;
    swap(x,y);
    ...
}

Guess you like

Origin www.cnblogs.com/wangcl97/p/11332225.html