Variable Scope in C++?

前端 未结 4 1379
半阙折子戏
半阙折子戏 2021-01-27 14:52

In c++, any variables declared in main will be available throughout main right? I mean if the variables were declared in a try loop, will they would still be accessible througho

4条回答
  •  误落风尘
    2021-01-27 15:37

    The scope of number is limited to the try block. Pull out this declaration to the main scope to access the variable after the try block:

    int main()
    {
       int number = 0;
       try 
       {
         // do something...
       }
    
       catch (...)
       {
         cout <<"Error";
       }
    
       number ++;
       cout <

提交回复
热议问题