标签:turn 空间 c++ wap 复制 int 实参 code 改变
#include <iostream> using std::endl; using std::cout; //引用函数作为参数 void swap(int a, int b)//形参,参数传递的方式是值传递-->就是进行复制 {//初始化形参:int a = x;int b = y; int temp = a; a = b; b = temp; }//不会交换x/y的值 //地址传递--》值传递 void swaq(int *pa, int *pb) { int temp = *pa; *pa = *pb; *pb = temp; }//地址传递 //引用传递 //好处:不会占用额外的空间,能够提升执行效率 void swal(int & ref1, int & ref2) {//形参初始化:int & ref1 = a;int & ref2 = b; int temp = ref1; ref1 = ref2; ref2 = temp; } int main() { int x = 10; int y = 20; cout << "x = " << x << endl; cout << "y = " << y << endl; swap(x , y);//实参 cout << "进行交换之后" << endl; cout << "x = " << x<< endl; cout << "y = " <<y<<endl; swaq(&x,&y);//实参 cout << "进行交换之后" << endl; cout << "x = " << x << endl; cout << "y = " << y << endl; swal(x, y);//实参 cout << "进行交换之后" << endl; cout << "x = " <<x<< endl; cout << "y = " <<y<< endl; system("pause"); return 0; }
#include <iostream> using std::endl; using std::cout; int arr[5] = { 0,1,2,3,4 }; int & func(int idx) { return arr[idx]; } int main() { func(0) = 10; cout << arr[0] << endl; system("pause"); return 0; }
标签:turn 空间 c++ wap 复制 int 实参 code 改变
原文地址:http://www.cnblogs.com/lzxperson/p/7097212.html