指针或者引用作为形参

{
int temp;
//使用指针为形参的情况下需要将以下的a和b分别替换为
a和*b
temp = a;
a = b;
b = temp;
}

swap(x, y);
//…main函数其它部分…
void swap(int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
}

int px = &x, py = &y;
swap(px, py);
//…main函数其它部分…
void swap(int
a, int
b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}

*a的值就是x的值（即4），temp获得4这个值。------------------------------------------------------------------------------------------------------------------------------------------第六步：*a = *b;

------------------------------------------------------------------------------------------------------------------------------------------形参为引用的情况下：int x = 4, y = 5;
swap(x, y);
//…main函数其它部分…
void swap(int& a, int& b)
{
int temp;
temp = a;
a = b;
b = temp;
}

using namespace std;

int main()
{
int a = 1, b = 2; //声明并初始化两个int型变量
int *pa = &a, *pb = &b; //pa和pb分别为指向a和b的int型指针
int &ra = a, &rb = b; //ra和rb分别为指向a和b的int型引用

cout << "a*b=" << a*b <<endl;      //合法，输出为2
cout << "pa*pb=" << pa*pb << endl; //非法，编译报错
cout << "ra*rb=" << ra*rb << endl; //合法，输出同样为2

return 0;

}

using namespace std;

int main()
{
int a = 1, b = 2; //声明并初始化两个int型变量
int &ra = a, &rb = b; //ra和rb分别为指向a和b的int型引用
int &rra = ra, &rrb = rb; //rra和rrb分别为指向ra和rb的int型引用

cout << "a*b=" << a*b <<endl;          //合法，输出为2
cout << "ra*rb=" << ra*rb << endl;     //合法，输出同样为2
cout << "rra*rrb=" << rra*rrb << endl; //合法，输出依然为2

return 0;

}

11-05 3499
08-15 4338

12-07 1553
08-01 334
08-02 1949
07-05 661
05-22 1404
08-10 1万+
03-05 3万+
03-16 1772
06-06 5408
04-28 552
10-30 2339
09-18 4145
01-17 3889
05-04 4721