我是靠谱客的博主 负责硬币,这篇文章主要介绍[C++]reference variables(引用变量)在pass arguments to functions中的运用,现在分享给大家,希望可以做个参考。
一. reference variable的形式
int a;
int & b = a; // b is alias(别名) for a
[例]
复制代码
1
2
3
4
5
6
7
8
9#include <iostream> using namespace std; int main(int argc, const char * argv[]) { int a=10; int & b = a; cout<<b<<"n"; return 0; }
the output is:
复制代码
1
210
二. reference variables(引用变量)在pass arguments to functions中的运用
[例]
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13#include <iostream> using namespace std; void func(int & x) { x=x+10; } int main() { int m=10; func(m); cout<<"the output is: "<<m<<"n"; return 0; }
x成为了m的别名(alias),所以x加10即m加10,因此结果为m变为20.
复制代码
1
2the output is: 20
最后
以上就是负责硬币最近收集整理的关于[C++]reference variables(引用变量)在pass arguments to functions中的运用的全部内容,更多相关[C++]reference内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复