Comparison operator overloading

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

    In general, implementation 2 is better for many reasons. First of all, you don't write (almost) duplicate code. If you need to change it (because the class has grown or there has been a bug), again with implementation 2 you change only 1 place. That is, implementation 2 makes your code more consistent and less error prone.

提交回复
热议问题