fun(int &a)
{
}
main()
{
........
int a = 5 ;
fun(a);
}
调用函数fun,传变量a的引用,在main中使用a,和在fun中使用a的效果是一样的
可不可以理解为变量a的作用域从main扩展到fun
伊谢尔伦2017-04-17 13:45:25
The concept of scope is the scope of declaration. It has nothing to do with the instance.
迷茫2017-04-17 13:45:25
No. Simply change the parameter name to fun(int &b), can the scope of variable a be extended to fun
PHP中文网2017-04-17 13:45:25
Purely guessworkWhy does C++ choose & as the reference symbol? I think reference is actually a kind of address, which can be compared to a pointer. A space will be opened in the fun function stack to store the address of variable a. To access a in fun, you may access a indirectly through the address
天蓬老师2017-04-17 13:45:25
There is no variable scope expansion, it is just a reference transfer of variables