Comparison operator overloading

前端 未结 5 1371
忘了有多久
忘了有多久 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:35

    Implementation 2 is better because it makes use of the already defined operator==. Also those operator functions should be const because they don't modify the object.

提交回复
热议问题