Summary of value passing, pointer passing, and reference passing in C + +pointer passing and reference passing generally apply to: The function modifies the parameters internally and wants the changes to affect the caller. In contrast, pointer/reference passing can alter the change from a formal parameter to an argument (which is actually modified directly on the memory of the argument, not as the value is passed to copy the value of the argument to another memory address). Another use of pointer/reference passing is that when a function actually needs to return multiple values, but only explicitly return a value, you can pass the variable that needs to be returned as a pointer/reference to the function, so that after the function is modified and returned, the caller can get the modified variable. It is also equivalent to an implicit return value pass.
1. Value passing: The parameter is a copy of the argument, and changing the value of the parameter does not affect the value of the external argument. From the point of view of the called function, a value pass is one-way (argument-and-parameter), and the value of the argument can only be passed in and not outgoing. The value is passed when the parameter needs to be modified inside the function and you do not want the change to affect the caller.
voidSwapintAintb) { inttemp; Temp=A; A=b; b=temp; cout<<a<< ' <<b<<' \ n ';}intMain () {intx=1; inty=2; Swap (x, y); cout<<x<< ' <<y<<' \ n '; return 0;}
Debugging with GDB found that the address of X, Y is 0xffbef938, 0xffbef934, respectively, the value is 1, 2. The address of the formal parameter, a, B, is 0xffbef918,0xffbef914, although they store the same value as X, Y, 1, 2, but this is just a copy. Swap only swaps A A, B, and does not change the value of x, Y. Output is 2,1;1,2
2. Pointer passing:
void swap (int *a,int *b) { int temp; Temp=*A; *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,1. parameter x, y, formal parameter, a, B, the address of the same, but the content of a, B is 0xffbef938 (address of X), 0xffbef934 (Address of Y), *a is the 0xffbef938 memory of the content, that is, the value of x 1. Simply put, A is a pointer to an external argument address, *a is the content of the pointer, and if changing the *a will inevitably result in changes to the external arguments.
3. Reference Delivery:
voidSwapint&a,int&b) { inttemp; Temp=A; A=b; b=temp; cout<<a<< ' <<b<<' \ n ';}intMain () {intx=1; inty=2; Swap (x, y); cout<<x<< ' <<y<<' \ n '; return 0;}
The output is 2,1;2,1. The address of the actual parameter x, Y is ibid. However, unlike pointer passing, the address of the formal parameter, a, B, is the same as x, Y, 0xffbef938, 0xffbef934. In this way, exchanging a a A is equivalent to exchanging x, Y. As for the pointer/reference pass format, you can refer to the following content:
int x=1; int // for pointer passing, Y has its own independent memory address, the stored content is the address of X, *y is the value of x int // for reference passing, it can be understood that Z is x,x or z, except that the name is different.
One last example:
intChange1 (Char*name) {Name="Alter"; return 1;}intChange2 (Char* &name) {Name="Alter"; return 1;}intMain () {Char*string= "original!"; Change1 (string); cout<<string<<' \ n '; Change2 (string); cout<<string<<' \ n ';}
Results: original!; Alter. Change1 is a value pass, and the parameter name has its own independent memory address, which is the contents of the copy string (the "original" address of the string), and the modified name becomes the address of "alter". Change2 is a reference pass, the address of the parameter name is the address of a string, or name is a string
Summary of value passing, pointer passing, and reference passing in C + +