Declaring and initializing a variable in a Conditional or Control statement in C++

后端 未结 9 1439

In Stroustrup\'s The C++ Programming Language: Special Edition (3rd Ed), Stroustrup writes that the declaration and initialization of variables in the conditionals

9条回答
  •  醉酒成梦
    2020-11-30 11:24

    To complement the other folks' good answers, you can always limit the scope of the variable by braces:

    {    
      const int readResult = read(socket);
      if(readResult < 0) {
        // handle error
      } 
      else if(readResult > 0)
      {
        // handle input
      } 
      else {
        return true;
      } 
    }
    

提交回复
热议问题