Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does overloading '==' get you '!='?

Tags:

c++

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.

like image 761
prelic Avatar asked Nov 03 '11 19:11

prelic


People also ask

Are overloading muscles good?

Progressive Overload is an important principle in strength training. Without progressive overload you won't build strength and you won't gain muscle. However, too much overload can lead to under-recovery and injury.

What happens when muscles are overloaded?

Overload is placing your muscles under greater stress than they're accustomed to. As a result, your muscles adapt so they can handle the added stress. This involves the synthesis of new myofibrils, the contractile elements that make up a muscle fiber and cause it to contract.

What does overload muscle mean?

Put simply, overload is what makes your muscles grow. The only way your body changes is if the muscles are taxed to the point where they must grow stronger to lift that weight. Overload causes the muscle fibers to become strong enough to handle the extra resistance.


Video Answer


2 Answers

Overloading 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 operator !=.

(1) To know more about it read Three basic rules of operator overloading in C++.

like image 64
K-ballo Avatar answered Sep 22 '22 12:09

K-ballo


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.

NO.
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.

like image 26
Alok Save Avatar answered Sep 22 '22 12:09

Alok Save