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

后端 未结 9 1442

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

    Adding to what RedGlyph and Ferruccio said. May be we can do the following to still declare within a conditional statement to limit its use:

    if(int x = read(socket)) //x != 0
    {
      if(x < 0) //handle error
      {}
      else //do work
      {}
    }
    else //x == 0  
    {
      return true;
    }
    

提交回复
热议问题