Suppose I have a class with several member variables:
class MyClass{
std::string a;
int b;
SomeOtherClass c;
// some stuff...
public:
// some other stuff...
};
I want to define relational operators (operator<
, etc.) that first compare a
, but if the a
are equal, compare b
, but if the b
are equal, compare c
. (We assume SomeOtherClass
already has relational operators defined.) So I have something like
bool operator==(MyClass param){
return (a == param.a) && (b == param.b) && (c == param.c);
}
bool operator<(MyClass param){
if(a < param.a) return true;
if(a > param.a) return false;
if(b < param.b) return true;
if(b > param.b) return false;
if(c < param.c) return true;
return false;
}
and so on. Is there any more elegant way to do this? It seems quite cumbersome, especially if there are lots of member variables to be compared. (Boost is an option.)