I have to overload a == operator in C++ for a class with many attributes.
The operator should return true, if and only if all attributes are equal. A shortcut might be useful, if these attributes change over time, to avoid bugs.
Is there a shortcut to compare every attribute in a class?
Relational Operators Overloading in C++You can overload any of these operators, which can be used to compare the objects of a class. Following example explains how a < operator can be overloaded and similar way you can overload other relational operators.
Operator overloading is a crucial concept in C++ that lets you achieve the functionality of the built-in operators while working with user-defined data types. Comparison operators in C++ are the ones that are there to compare two values with each other such as “==”, “!= ”, “>”, “<”, “>=”, and “<=”.
C++ Program to overload the Greater than > operator In this program we try to overload the > operator with C++. Greater number C++ Program with operator overloading. n1 is greater than n2.
Assignment Operators Overloading in C++You can overload the assignment operator (=) just as you can other operators and it can be used to create an object just like the copy constructor. Following example explains how an assignment operator can be overloaded.
There is no shortcut. You will have to list everything.
Some sources of error can be reduced by introducing a member function named tied()
like:
struct Foo { A a; B b; C c; ... private: auto tied() const { return std::tie(a, b, c, ...); } };
So that your operator==
can just use that:
bool operator==(Foo const& rhs) const { return tied() == rhs.tied(); }
This lets you only list all your members once. But that's about it. You still have to actually list them (so you can still forget one).
There is a proposal (P0221R0) to create a default operator==
, but I don't know if it will get accepted.
The above proposal was rejected in favor of a different direction regarding comparisons. C++20 will allow you to write:
struct Foo { A a; B b; C c; // this just does memberwise == on each of the members // in declaration order (including base classes) bool operator==(Foo const&) const = default; };
Starting in C++11 with the introduction of tuples we also got std::tie()
. This will let use make a tuple out of a bunch of variables and call a comparison function against all of them. You can use it like
struct Foo { int a,b,c,d,e,f; bool operator==(const Foo& rhs) { return std::tie(a,b,c,d,e,f) == std::tie(rhs.a,rhs.b,rhs.c,rhs.d,rhs.e,rhs.f); } };
You still have to list all the members you want to check but it makes it easier. You can also use this to make less than and greater than comparisons much easier.
It should also be noted that the variables are checked in the order you provide them to tie
. This is important for less than and greater than comparisons.
std::tie(a,b) < std::tie(rhs.a, rhs.b);
Does not have to be the same as
std::tie(b,a) < std::tie(rhs.b, rhs.a);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With