Using a variable with the same name in different spaces

前端 未结 4 719
走了就别回头了
走了就别回头了 2020-12-20 10:51

This code compiles, but I have a run time error in Visual Studio:

Run-time check failure #3 - the variable \'x\' is being used without being initializ

4条回答
  •  Happy的楠姐
    2020-12-20 11:42

    When you declare a new variable, its name becomes visible right here

    int x =
    //     ^- there
    

    because it is at that point the variable is fully declared, and as such; its name means something. At this point in time any other (previously declared variable) in a surrounding scope will be hidden.

提交回复
热议问题