小白教程
所有教程
关于
Search
172.69.59.44
172.69.59.44
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Passing parameters by references”的源代码
本页内容
下一节:
Returning_values_by_reference
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:把引用作为参数}}[[Category:cpp references|1]] = C++ 把引用作为参数 = [[cpp references|C++ 引用]] 我们已经讨论了如何使用指针来实现引用调用函数。下面的实例使用了引用来实现引用调用函数。 <sample title="" desc="" lang="c" hererun="1"> #include <iostream> using namespace std; // 函数声明 void swap(int& x, int& y); int main () { // 局部变量声明 int a = 100; int b = 200; cout << "交换前,a 的值:" << a << endl; cout << "交换前,b 的值:" << b << endl; /* 调用函数来交换值 */ swap(a, b); cout << "交换后,a 的值:" << a << endl; cout << "交换后,b 的值:" << b << endl; return 0; } // 函数定义 void swap(int& x, int& y) { int temp; temp = x; /* 保存地址 x 的值 */ x = y; /* 把 y 赋值给 x */ y = temp; /* 把 x 赋值给 y */ return; } </sample> 当上面的代码被编译和执行时,它会产生下列结果: <pre> 交换前,a 的值: 100 交换前,b 的值: 200 交换后,a 的值: 200 交换后,b 的值: 100 </pre> [[cpp references|C++ 引用]]
返回至“
Passing parameters by references
”。
下一节:
Returning_values_by_reference