Home  >  Q&A  >  body text

c++ - 变量作用域可不可以扩展

fun(int &a)  
{
         
}


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

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

大家讲道理大家讲道理2764 days ago967

reply all(5)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-17 13:45:25

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

    reply
    0
  • 迷茫

    迷茫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

    reply
    0
  • PHP中文网

    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

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 13:45:25

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

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 13:45:25

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

    reply
    0
  • Cancelreply