Dave's Brain

Browse - programming tips - c++ make a less than or equal operator from existing operators

Date: 2010feb26
Language: C/C++

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 of C++ made this automatically for you.
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2010, 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.