11
General Software Discussion / Re: Stupid question ... what is the difference between != and <>?
« on: August 22, 2011, 01:23 AM »
There is one potential reason that some languages (C++ springs to mind) would have a difference between !(x==y) and x!=y
When making a class in c++ (And some derived languages), you may overload the == operator. Doing so allows you to check if two instances are equal, even if they are not references to the same instance. The thing you have to remember is that the C++ compiler will not automatically overload the != operator for you. As such, if the programmer of a class has forgotten to include something similar to the following,
So yes, when dealing with poorly written code, there is a difference between the two, but only in some languages, and only if the class in question wasn't implemented properly.
When making a class in c++ (And some derived languages), you may overload the == operator. Doing so allows you to check if two instances are equal, even if they are not references to the same instance. The thing you have to remember is that the C++ compiler will not automatically overload the != operator for you. As such, if the programmer of a class has forgotten to include something similar to the following,
bool MyClass::operator!=(const MyClass &other) const {
return !(*this == other);
}
then !(x==y) will use your equality operator, while x!=y will simply check if they are the same instance.return !(*this == other);
}
So yes, when dealing with poorly written code, there is a difference between the two, but only in some languages, and only if the class in question wasn't implemented properly.