Summary of C ++ median transfer, pointer transfer, and reference Transfer

Source: Internet
Author: User

1. Value Transfer: The shape parameter is a copy of the real parameter. Changing the value of the shape parameter does not affect the value of the external real parameter. From the perspective of the called function, value passing is unidirectional (real parameter-> parameter). The parameter value can only be passed in and cannot be passed out. When the function needs to modify parameters internally and does not want this change to affect the caller, the value is passed.

Void swap (int A, int B)
{
Int temp;
Temp =;
A = B;
B = temp;
Cout <A <<''' <B <'\ n ';
}

Int main (){
Int x = 1;
Int y = 2;
Swap (x, y );
Cout <x <''<Y <'\ n ';

Return 0;
}

After debugging with GDB, we found that the addresses of X and Y are 0xffbef938 and 0xffbef934, with values of 1 and 2 respectively. The addresses of parameters A and B are 0xffbef918 and 0xffbef914 respectively. Although the values stored in them are the same as those in X and Y, they are both 1 and 2, but they are only copied. Swap only exchanges a, B, and does not change the value of X and Y. The output is ;.

2. pointer transmission:

Void swap (int * a, int * B)
{
Int temp;
Temp = *;
* A = * B;
* B = temp;
Cout <* A <<''' <* B <'\ n ';
}

Int main (){

Int x = 1;
Int y = 2;
Swap (& X, & Y );
Cout <x <''<Y <'\ n ';

}

The output result is 2, 1, 2, and 1. The addresses of the real parameters X, Y, and form parameters A and B are the same as those above, but the contents of A and B are 0xffbef938 (address of X), 0xffbef934 (address of Y ), * A is the content stored in the memory of 0xffbef938, that is, the value of X is 1. To put it simply, a is a pointer pointing to the external real parameter address, and * A is the content of the pointer. If * A is changed, it will inevitably lead to a change of the external real parameter.

3. transfer a reference:

Void swap (Int & A, Int & B)
{
Int temp;
Temp =;
A = B;
B = temp;
Cout <A <<''' <B <'\ n ';
}

Int main (){

Int x = 1;
Int y = 2;
Swap (x, y );
Cout <x <''<Y <'\ n ';
Return 0;
}

The output is 2, 1, 2, and 1. The address of X and Y is the same as the preceding one. However, unlike pointer passing, the addresses of the form parameters A and B are the same as those of X and Y, that is, 0xffbef938 and 0xffbef934. In this way, the exchange of A and B is equivalent to the exchange of X and Y.

Pointer passing and reference passing are generally applicable to modifying parameters in a function and affecting callers. When the comparison value is passed, the pointer/reference transmission can pass the change by the form parameter to the real parameter (in fact, it is directly modified on the memory of the real parameter, unlike passing values to copy the values of real parameters to another memory address ). Another usage of pointer/reference passing is: when a function actually needs to return multiple values, but only one value can be explicitly returned, another variable to be returned can be passed to the function as a pointer/reference. After the function is modified and returned, the caller can get the modified variable, it is also equivalent to passing an implicit return value.

For the format of pointer/reference transfer, refer to the following content:

Int x = 1;

Int * Y = & X; // used for pointer transmission. Y has its own memory address. The stored content is the address of X, and * Y is the value of X.

Int & Z = x; // used for reference transfer. It can be understood that Z is X, X is Z, but the name is different.

Finally, let's look at another example:

Int Change1 (char * Name ){
Name = "alter ";
Return 1;
}

Int Change2 (char & name ){
Name = "alter ";
Return 1;
}

Int main (){
Char * string = "Original !";
Change1 (string );
Cout <string <'\ n ';

Change2 (string );
Cout <string <'\ n ';

}

Result: original !; Alter. Change1 is the value transfer, and the name of the parameter has its own memory address. The content is the copy string content (the string content is the original address ), after modification, the content of the name changes to the "alter" address. Change2 is the reference transmission, and the name of the form parameter is the string address, or the name is the string address.

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.