c++ - 变量作用域可不可以扩展
大家讲道理
大家讲道理 2017-04-17 13:43:25
0
5
989
fun(int &a)  
{
         
}


main()   
{     
    ........     
    int a = 5 ;    
    fun(a);    
}

调用函数fun,传变量a的引用,在main中使用a,和在fun中使用a的效果是一样的
可不可以理解为变量a的作用域从main扩展到fun

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(5)
伊谢尔伦

The concept of scope is the scope of declaration. It has nothing to do with the instance.

迷茫

No. Simply change the parameter name to fun(int &b), can the scope of variable a be extended to fun

洪涛

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

Peter_Zhu

There is no variable scope expansion, it is just a reference transfer of variables

小葫芦

This question is so retarded, I want to ask you how you learned it

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template