Dave's Brain

Browse - Programming Tips - Can I make a = operator from existing and == operators in my class?

Date: 2010feb26
Language: C/C++
Keywords: lessthan, greaterthan, equals

Q.  Can I make a <= operator from existing < and == operators in my class?

A.  Yes, like this:

bool operator<=(const MyClass &b) const { return operator<(b) || operator==(b); } bool operator>=(const MyClass &b) const { return operator>(b) || operator==(b); }
I think it would be cool if C++ made this automatically for you.

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.