Suppose I have a class with several member variables:
class MyClass{ std::string a; int b; SomeOtherClass c;
I want to define relational operators ( operator< , etc.) that first compare a , but if a are equal, compare b , but if b are equal, compare c . (We assume SomeOtherClass already has relational operators.) 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; }
etc. Is there a more elegant way to do this? This seems rather cumbersome, especially when comparing multiple member variables. (Boost is an option.)
source share