这些 (bCondition == NULL) 和 (NULL==bCondition) 之间有什么区别?

2021-12-12 00:00:00 c if-statement syntax c++

在浏览 msdn 站点时,他们使用的大多数条件检查位置 (NULL == bCondition).

While exploring msdn sites ,most of the condition checking places they are using (NULL == bCondition).

使用这些符号的目的是什么?

what is the purpose of using these notations?

请提供一些示例来解释这些.

Provide some sample to explain these please.

谢谢.

推荐答案

当赋值运算符 = 被意外使用而不是比较运算符 ==:

The use of NULL == condition provides more useful behaviour in the case of a typo, when an assignment operator = is accidentally used rather then the comparison operator ==:

if (bCondition = NULL)  // typo here
{
 // code never executes
}

if (NULL = bCondition) //  error -> compiler complains
{
 // ...
}

C-compiler 在前一种情况下会给出警告,很多语言都没有这样的警告.

C-compiler gives a warning in the former case, there are no such warnings in many languages.

相关文章