# include <iostream.h>
void swap_putong(int x,int y) //普通形式交换两个变量!(这里变量用的是小写!)
{ //普通
int temp;
temp=x;
x=y;
y=temp;
}
void swap_yinyong(int & x,int & y) //引用形式交换两个变量!(这里变量用的是小写!)
{ //引用
int temp;
temp=x;
x=y;
y=temp;
}
void swap_zhizhen(int *x,int *y) //指针形式交换两个变量!(这里变量用的是小写!)
{ //指针
int temp;
temp=*x;
*x=*y;
*y=temp;
}
void main()
{
int X=3; //这里变量用的是大写!
int Y=9;
cout<<"X="<<" "<<X<<endl;
cout<<"Y="<<" "<<Y<<endl;
swap_putong(X,Y);
cout<<"after swap_putong...: \n";
cout<<"X="<<" "<<X<<endl;
cout<<"Y="<<" "<<Y<<endl;
swap_yinyong(X,Y);
cout<<"after swap_yinyong...: \n";
cout<<"X="<<" "<<X<<endl;
cout<<"Y="<<" "<<Y<<endl;
swap_zhizhen(&X,&Y);
cout<<"after swap_zhizhen...: \n";
cout<<"X="<<" "<<X<<endl;
cout<<"Y="<<" "<<Y<<endl;
}
问题1:swap_putong //普通
swap_yingyong //引用
swap_zhizhen //指针
此三个函数是用怎样的机理去操纵主函数中的变量的呢?
问题2:
swap_yingyong
swap_zhizhen
操作机理重要区别在那里呢?
能从这里还能达到什么启发呢?
[此贴子已经被作者于2006-8-30 11:57:38编辑过]