If I manually overload the
== operator for a structure, do I get the
!= operator for free (presumably defined to be the boolean opposite), or do I have to overload it manually (even if to just
return !(this == rhs)?
Edit-The question is not whether or not I CAN overload both operators, but whether I must overload inequality if I’ve already overloaded the equality operator. Regardless, good answers have been given.
operator == does not give you
operator !=. You have to do it manually and the canonical way is to implement it in terms of
operator == as in
!(left == right).
The semantics of the operators are not dictated by the standard. You could very well overload
operator == to mean equality yet overload
operator != to something different like addition or even equality again (not that this is a good practice, in fact it should be discouraged. When in doubt, do as the ints do…).[Refer (1) Below]
On a side note,
Boost.Operators can help you provide canonical implementations for operators. There is also
std::rel_ops with a canonical implementation for
(1) To know more about it read Three basic rules of operator overloading in C++.
No nothing is for free. You pay for what you use in C++(in case of Operator Overloading).
You only get the Operator which you overload nothing more.
Also, It is a good practice that if you overload
== operator then you should overload
!= as well because the users of your class will expect that to be available.
Operator Overloading C++ FAQ should be a good read.
Answering the updated Question:
The question is not whether or not I CAN overload both operators, but whether I must overload inequality if I’ve already overloaded the equality operator.
There is no such requirement that you Must overload
!= If you need to overload
==. However,it is a good practice that you Should overload operators related to each other.
Why is it a good practice?
Think it from the perspective of the user of your class. If the user of your class can use
==(equality criteria) to compare objects of your class, naturally they are going to expect that they should be able to use
!=(Non-equality criteria) as well, this stems from the fact that these two operators are related closely and supported for all built-in tyes.
What happens if you disregard the
should and not overload
!= when you overload
If the users of your class use
!= they will get a compile error.
They would frown a bit about not being provided with
!= when they are provided with
== and they will have to realign their logic to use
== instead of the
So you can live with it but be ready to expect a few frowns and complaints of inconvinience and not providing user friendly interface.
You have to overload each operator. != and == are not linked.
It doesn’t, and thankfully it is the way it is. For example, you might want a structure, where both a!=b and a==a are true. They are not necessary the inverse, they can be anything you want.
Boosts the creativity. 🙂
For example if you don’t know the result of a comparison, or it is generally not known, then it would be reasonable to a==b and a!=b return the same.