Comparison operator overloading

前端 未结 5 1369
忘了有多久
忘了有多久 2020-12-30 18:59

Which is best practice (in this case):

bool Foo::operator==(const Foo& other) {
  return bar == other.bar;
}

// Implementation 1
bool Foo::operator!=(co         


        
5条回答
  •  天命终不由人
    2020-12-30 19:46

    The second implementation has the notable constraint that == will always be the boolean opposite of !=. This is probably what you want, and it makes your code easier to maintain because you only have to change one implementation to keep the two in sync.

提交回复
热议问题