Difference between local scope and function scope

前端 未结 5 1813
独厮守ぢ
独厮守ぢ 2020-12-20 19:05

Once I assumed that these two have the same meaning but after reading more about it i\'m still not clear about the difference. Doesn\'t the local scope sometimes refer to sc

5条回答
  •  别那么骄傲
    2020-12-20 19:33

    bool m[3][3];
    void f1()
    {
      int i;
      // redefining a variable with the same name in the same scope isn't possible
      //int i; //error C2086: 'int i' : redefinition
    }
    
    void f2()
    {
      int i; // ok, same name as the i in f1(), different function scope.
    
      {
        int i; // ok, same name as the i above, but different local scope.
      }
    
      // the scope if the following i is local to the for loop, so it's ok, too.
      for (int i = 0; i < 3; i++)
      {
        for (int j = 0; j < 3; j++)
        {
          if (m[i][j])
            goto loopExit;
        }
      }
    loopExit:
      std::cout << "done checking m";
    // redefining a label with the same name in the same function isn't possible
    // loopExit:; // error C2045: 'loopExit' : label redefined
    }
    
    void f3()
    {
    loopExit:; // ok, same label name as in f2(), but different function scope
    }
    

提交回复
热议问题