1、引用传递
注意函数中,function(int a);function(int *a);function(int &a);三者的区别
#include <iostream>
#include<string>
using namespace std;
void Swap1(int a,int b)
{
int temp=a;
a=b;
b=temp;
cout<<"Swap1中 a:"<<a<<" b: "<<b<<endl;
}
void Swap2(int *a,int *b)
{
int temp= *a;
*a=*b;
*b=temp;
}
void Swap3(int &a,int &b)
{
int temp=a;
a=b;
b=temp;
}
int main()
{
int a=10;
int b=20;
Swap1(10,20); //值传递,形参不会修饰实参
Swap2(&a,&b); //地址传递,形参会修饰实参的
Swap3(a,b); //引用传递,形参会修饰实参的
cout<<"Swap3中 a:"<<a<<" b: "<<b<<endl;
return 0;
}