Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Only writing == and > operators, but having all of them 1

Status
Not open for further replies.

burgdavid

Programmer
Jun 22, 2000
70
DE
You want to make comparison operators from our class ? Only write == and &gt; (any other combinaison), then you can deduce others using the two firsts :<br><br>!= is not ==<br>&lt; is not &gt; and not ==<br>etc...<br><br>Perfomance will be of course increase by writing every comparison method, but this will take you much time for a little increase. Just try to use directly the two comparison you write, and not include other like this :<br><br>=&lt; is &lt; or == (will become (not &gt; and not ==) or ==, which is not very clever...)<br><br>Simple but maybe usefull,<br><br>David.
 
David,<br><br>Is there a question in there somewhere?<br><br>Otherwise, a common technique for implementing comparison operators is to first create a 'compare' function that returns -1, 0, 1 depending on the comparison. Then all the operators use compare() to obtain their results. They can usually be inlined for optimization.<br><br>&quot;But, that's just my opinion... I could be wrong&quot;.<br>-pete<br>
 
Pete,<br><br>no there is no question here : look at the icon on the message, it's not a question mark but a lighting bulb. I've see that when posting a thread you can choose different icons, and I was fearing to use too much the question mark this days. So I decide to try to post a tip.<br>But, by the way, your method looks better !<br><br>David.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top