Does overloading '==' get you '!='?

前端 未结 4 1970
慢半拍i
慢半拍i 2020-12-15 02:27

If I manually overload the == operator for a structure, do I get the != operator for free (presumably defined to be the boolean opposite), or do I

4条回答
  •  一向
    一向 (楼主)
    2020-12-15 02:50

    It doesn't, and thankfully it is the way it is. For example, you might want a structure, where both a!=b and a==a are true. They are not necessary the inverse, they can be anything you want.

    Boosts the creativity. :)

    For example if you don't know the result of a comparison, or it is generally not known, then it would be reasonable to a==b and a!=b return the same.

    Example: http://en.wikipedia.org/wiki/Three-valued_logic

提交回复
热议问题