#includeusing namespace std; //交换函数 //1、值传递 void mySwap01(int a, int b) { int temp = a; a = b; b = temp; cout <<"mySwap01的a:"<< a << endl; cout <<"mySwap01的b:"<< b << endl; cout << endl; } //2、地址传递 void mySwap02(int* c, int* d) { int temp = *c;; *c = *d; *d = temp; cout << "mySwap02的c:" << *c << endl; cout << "mySwap02的d:" << *d << endl; cout << endl; } //3、引用传递 void mySwap03(int &e,int &f) { int temp = e; e = f; f = temp; cout << e << endl; cout << f << endl; } int main(void) { int a = 1; int b = 2; mySwap01(a, b);//值传递,改变了形参,没有改变实参,即形参不会修饰实参 cout << "a实际值;" << a << endl; cout << "b实际值;" << b << endl; cout << endl; int c = 3; int d = 4; mySwap02(&c, &d);//地址传递,形参会修饰实参 cout << "c实际值;" << c << endl; cout << "d实际值;" << d<< endl; cout << endl; int e = 0; int f = 9; mySwap03(e, f);//引用传递,形参会修饰实参 cout << "e实际值;" << e << endl; cout << "f实际值;" << f << endl; system("pause"); return 0; }



