Advertisement

How to create a operator in C++

Started by March 05, 2001 01:34 PM
4 comments, last by Zeblar Nagrim 23 years, 11 months ago
I getting really frustrated on the fact that the POINT structure dosn´t have a == operator! That''s why I want to learn how to create my own operators... Please show me some code. Zeblar Nagrim, Lord of Chaos
  bool operator == (const POINT& lhs, const POINT& rhs){  if( (lhs.x == rhs.x) && (lhs.y == rhs.y) )    return true;  else    return false;}POINT a,b;if(a==b)  doSomething();[source]its not really  hard stuff, its like a regular function,just replace the name with "operator x" and use logical return type and arguements  


Just because the church was wrong doesn''t mean Galileo wasn''t a heritic.
It just means he was a heritic who was right.
Just because the church was wrong doesn't mean Galileo wasn't a heretic.It just means he was a heretic who was right.
Advertisement
A possibly slightly faster version of the above code would be (uses short circuit evaluation):

      inline bool operator == (const POINT& lhs, const POINT& rhs){      if( (lhs.x != rhs.x) || (lhs.y != rhs.y) )        return false;              return true;}  



Dire Wolf
www.digitalfiends.com

Edited by - Dire.Wolf on March 6, 2001 1:50:31 AM
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
It doesn''t have a == operator or any other operators for that matter is so that it can be used in C as well. If you want operator overloading, you could do it yourself, or use MFC''s CPoint
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
Thank you Atavist for the explanation and Dire.Wolf for the optimization. Thank you guys!

Greetings,



Zeblar Nagrim, Lord of Chaos
Heh, my fault. It would have evaluated to the same optimization either way

Dire Wolf
www.digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com

This topic is closed to new replies.

Advertisement