I am trying to overload relational operators(==, >, <, >=, <=), but I want to be able to return false if the two objects are different, rather than returning an error. For example, I have an ArrayList class and a Vector class that currently cannot be compared. How can I accomplish this?
Asked
Active
Viewed 606 times
-3
-
It's normal for `==` to return `false` when the objects differ, and it would not be normal for any of them to "return an error" (whatever you mean by that). Can you elaborate on what problem you are having exactly? – M.M Nov 02 '17 at 01:48
-
1Possible duplicate of [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) – user0042 Nov 02 '17 at 01:48
-
I think he means he wants to be able to compare objects of different types, which is not something that is often implemented (and so would result in a compile-time error) for user-defined types. – Benjamin Lindley Nov 02 '17 at 01:53
-
Sure, I have a few classes that overload these operators and work fine with the functions in their classes which only take in one object (eg. bool operator==(const ArrayList& a)), but I want to know if I can, in a separate class, overload them to check any object against any object (eg. my ArrayList class against my Vector class). As it stands, I get an 'invalid operands to binary expression' error – Seth Myers Nov 02 '17 at 01:56
-
1This is an obvious XY problem. What is the real problem are you trying to solve. No, not the one about overloading comparison operators this way, but the real problem to which you believe the solution is to overload comparison operators this way. Because, otherwise, you propose that 5==5 will be false, if the first 5 is an `int`, and the second 5 is a `char`. Different types, but this makes no sense, whatsoever. This is not your real problem. You need to explain, cogently, what your real question itself. – Sam Varshavchik Nov 02 '17 at 01:56
-
If you want a comparison operator to return false if one, specific type is compared to a different, specific type, then simply declare this specific comparison operator to always return false. Mission accomplished. – Sam Varshavchik Nov 02 '17 at 01:58
1 Answers
1
Try maybe something like:
#include <iostream>
class ArrayList {};
class Vector {};
bool operator==(Vector const &v, ArrayList const &al) { return false; }
bool operator==(ArrayList const &al, Vector const &v) { return false; }
int main()
{
ArrayList al;
Vector v;
std::cout << (al == v ? "true" : "false") << std::endl;
std::cout << (v == al ? "true" : "false") << std::endl;
return 0;
}
Both comparisons will print false;
EDIT Using templates:
#include <iostream>
#include <type_traits>
class ArrayList {};
class Vector {};
class Whatever {};
template <class T, class U> bool operator==(T const &, U const &)
{
return std::is_same<T, U>::value;
}
int main()
{
ArrayList al;
Vector v;
Whatever w;
std::cout << (al == v ? "true" : "false") << std::endl;
std::cout << (v == al ? "true" : "false") << std::endl;
std::cout << (w == v ? "true" : "false") << std::endl;
std::cout << (w == w ? "true" : "false") << std::endl;
return 0;
}
This will simply return true if classes are the same or false if not.

Killzone Kid
- 6,171
- 3
- 17
- 37
-
yeah that will work for that specific case, to be honest this is for an assignment question involving multiple classes but in the demo those are the two being compared. So there really isn't any overarching reason I need this done but I was wondering if it's possible for all classes. – Seth Myers Nov 02 '17 at 02:11
-