1, C + + Pass-through method:
(1) basic type parameter passing
int main () {int TT = 22;//transmission basic type Common (TT); cout << "tt value=" << tt << Endl;} int common (int a) {int i = 2;a = I;cout << a << endl;return 0;} The above code output: 2 2
(2) pointer type parameter passing
<span style= "White-space:pre" ></span>int main () {int tt = 22;int *P1 = &tt;//pass pointer int v = valuepass (p1); cout << "*p1=" << *p1 << Endl;} int valuepass (int *p1) {int a = 1;P1 = &a;cout << *p1 << endl;return 0;} <p> above code output: 1 <span style= "WHITE-SPACE:PRE;" ></span>22
(3) reference type parameter passing
int main () {int tt = 22;int *P1 = &tt;//pass pointer int v = valuepass (p1); cout << "*p1=" << *p1 << endl;//Transmission With V = Refpass (p1), cout << "*p1 refer=" << *p1 << Endl; }<span style= "font-family:arial, Helvetica, Sans-serif;" ></span><pre name= "code" class= "CPP" ><span style= "White-space:pre" ></span>int refPass ( int *&p1) {int a =1;p1 = &a;cout << *p1 << endl;return 0;}
<p> above code output: 1 </p><p><span style= "WHITE-SPACE:PRE;" > </span>1</p>
As you can see, the parameters from (1)---(2)--(3) are getting stronger, the range of parameter changes is increasing, (1) is a pure copy, (2) The address copy is passed, and (3) the variable itself passes.
In Java, however, only the usage of (1) and (2) is provided, but it is consistent with the way that C + + handles data.
Details of the Java parameter passing another time blog: http://blog.csdn.net/tangkund3218/article/details/18087543
The similarities and differences between C + + parameter passing and Java parameter passing