Or in other words, would these 2 blocks be equal for the compiler:
if (a!=b){
// do something
}
if (!(a==b)){
// do something
}
Or in other words, would these 2 blocks be equal for the compiler:
if (a!=b){
// do something
}
if (!(a==b)){
// do something
}
No. We can test this easily
#include <iostream>
class C {};
bool operator==(C, C) { return true; }
int main() {
C a, b;
std::cout << (a != b) << std::endl;
return 0;
}
Produces (on gcc)
.code.tio.cpp: In function ‘int main()’:
.code.tio.cpp:9:19: error: no match for ‘operator!=’ (operand types are ‘C’ and ‘C’)
std::cout << (a != b) << std::endl;
Operator overloads only give you what you are asking for, so overloading ==
will not affect !=
. Therefore, (a != b)
is not the same as (!(a == b))
.
However, good practice is to overload both to avoid confusion.