Which is best practice (in this case):
bool Foo::operator==(const Foo& other) {
return bar == other.bar;
}
// Implementation 1
bool Foo::operator!=(const Foo& other) {
return bar != other.bar
}
// Implementation 2
bool Foo::operator!=(const Foo& other) {
return !(*this == other);
}
For operators like >, <, <=, >= I would go with implementation 2 when possible. However, for != I think implementation 1 is better since another method call is not made, is this correct?
constto each one of the functions. Also consider using free functions rather than member functions, as the former are symmetric with respect to types and conversions where the latter aren't. This is more important if your type can be implicitly converted from other types.