可能重复:
在 C++ 的条件或控制语句中声明和初始化变量
而不是这个...
int value = get_value();
if ( value > 100 )
{
// Do something with value.
}
...是否可以将价值范围缩小到仅在需要的地方:
if ( int value = get_value() > 100 )
{
// Obviously this doesn't work. get_value() > 100 returns true,
// which is implicitly converted to 1 and assigned to value.
}