Chaining Bool values give opposite result to expected

后端 未结 5 1904
忘掉有多难
忘掉有多难 2020-12-17 15:32

Unthinkingly I wrote some code to check that all the values of a struct were set to 0. To accomplish this I used:

bool IsValid() {
    return !(0 == year ==          


        
5条回答
  •  再見小時候
    2020-12-17 15:43

    The return of the == operator is 1 if the operands are equal, so regardless wether this is read from left to right or right to left, this will not do what you expect.

    so this could only work in an analogous test if you would be interested if all values are 1.

    And to have a shorter expression since you seem interested in that just do year || day || ...

提交回复
热议问题