Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

cMADsc

return value on operator overloading

This topic is 6233 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Greetings all, I have a operator overloading member function. It just compares two values. When the two values are compared shouldn''t a value be returned like "0" or "1" since it is a BOOL type? Thanks. ************************************************************** #include struct Point2d { float x; float y; bool operator<(const Point2d & p1); }; bool Point2d::operator <(const Point2d &p1) {return ((x < p1.x) && (y < p1.y));} int main() { Point2d pt1 = {10.0f, 15.0f}; Point2d pt2 = {110.0f, 8.0f}; cout << "example... " << endl; pt1 < pt2; return 0; } *********************************************************** ----------------------------- "There are ones that say they can and there are those who actually do." "...u can not learn programming in a class, you have to learn it on your own."

Share this post


Link to post
Share on other sites
Advertisement
Yes ... the result returned will be either ''true'' or ''false'', which when cast to integers will be ''1'' and ''0'' respectively. When casting from anything TO a bool .. it is important to know that a 0 (or 0.0 floating point) becomes ''false'' and anything non-zero becomes ''true'' ... so if you say:

while(4*2)

it will run forever, but if you say

while(3*0)

it will not run.

Share this post


Link to post
Share on other sites
I suspect your problem is this:

cout << "example... " << endl;
pt1 < pt2;

Try this:

cout << "example... " << endl << (pt1 < pt2) << endl;

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!