您的位置:首页 > 其它

社会十句经典大实话

2007-10-04 10:16 288 查看
一、值传递

#include "stdafx.h"

#include<iostream>

using namespace std;

//将两个数交换值

void swap(int a,int b){

int t=a;

a=b;

b=t;

}

int _tmain(int argc, _TCHAR* argv[])

{

int x=1,y=2;

cout<<"x="<<x<<" y="<<y<<endl;

swap(x,y);

cout<<"x="<<x<<" y="<<y<<endl;

return 0;

}

二、引用传递

#include "stdafx.h"

#include<iostream>

using namespace std;

//将两个数交换值

void swap(int &a,int &b){ //&a 、&b为实参的别名

int t=a;

a=b;

b=t;

}

int _tmain(int argc, _TCHAR* argv[])

{

int x=1,y=2;

cout<<"x="<<x<<" y="<<y<<endl;

swap(x,y);

cout<<"x="<<x<<" y="<<y<<endl;

return 0;

}

总结:

值传递中,形参是形参,实参是实参;

引用传递中,形参等于实参。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: