在C ++的If语句中,在等于==的左边写常量是一种好的风格吗?

时间:2015-08-03 07:29:39

标签: c++ coding-style

是否有关于在operator equal to ==中的比较If statement in C++左侧编写常量的建议,以避免在输入错误时输入错误。这是一个好习惯吗?或者这将降低代码的可读性,不建议这样做,因为如果条件表达式中的赋值将被写入,则存在Compiler Warning (level 4) C4706

const int maxValue = 15;

1)

if (currentValue == maxValue)
{
    // do something 
}

如果等于==的运算符将错误输入赋值运算符=,currentValue = maxValue if将始终为true,因为任何非零值为“true”

或者更好地写

2)

if (maxValue == currentValue)
{
   // do something 
}

0 个答案:

没有答案