I am currently browsing the old C ++ codebase and see that a lot of code looks like this:
bool SomeClass::operator==( const SomeClass& other ) const
{
return member1 == other.member1 && member2 == other.member2;
}
bool SomeClass::operator!=( const SomeClass& other ) const
{
return member1 != other.member1 || member2 != other.member2;
}
it is clear that the comparison logic is duplicated, and the code above will probably have to be changed in two places, and not in one.
AFAIK typical implementation method is operator!=as follows:
bool SomeClass::operator!=( const SomeClass& other ) const
{
return !( *this == other );
}
In the latter case, any change in logic occurs in operator==, it is automatically reflected in operator!=, because it simply causes operator==and executes the negation.
Is there any resonant case when it operator!=should be implemented in any other way than just reuse operator==in C ++ code?