Reason for the existence of non-short-circuit logical operators

后端 未结 6 1233
借酒劲吻你
借酒劲吻你 2020-11-28 08:16

When used with boolean operands, & and | become logical operators per Section 15.22.2 of the JLS. Unlike &&

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-28 08:42

    My case (C++):

    void setFields(Parameters bundle)
    {
      if (setIfDifferent(&field1, bundle.value1) | 
          setIfDifferent(&field2, bundle.value2) |
          setIfDifferent(&field3, bundle.value3)) {
        storeState();
      }
    }
    

    setIfDifferent() sets the object's field with new value if they differ, in which case it returns true; or it returns false in case the field's and the new value are the same. So, we want to try to set all fields, and if any of them changed, then we want to store new object's state.

提交回复
热议问题