求解這個(gè)符合“&'
#include<iostream> #include<stdlib.h> using?namespace?std; void?fun(int?&x,int?&y)?????//為什么這里需要&這個(gè)符號(hào),這個(gè)意味著什么 { ????int?a=0; ????a=x; ????x=y; ????y=a; } int?main(void) { ????int?x=10,y=20; ????cout<<x<<","<<y<<endl; ????fun(x,y); ????cout<<x<<","<<y<<endl; ????system("pause"); ????return?0; }
大神~~
2016-02-08
如果這里沒(méi)有&符號(hào),運(yùn)行了代碼之后,main函數(shù)中x和y的值并沒(méi)有發(fā)生交換。因?yàn)閙ain函數(shù)中的變量x、y與fun函數(shù)中的變量x、y作用域不同。