|
-
November 18th, 2008, 12:57 PM
#16
Re: Operator overloading
Code:
bool retval;
if( _first < p._first )
retval = true;
if(_first > p._first)
retval = false;
if(_first == p._first)
if(_i1 == p._i1)
if(_i2 == p._i2)
retval = false;
if(_i1 < p._i1)
retval = true;
if(_i1 > p._i1)
retval = false;
if(_i2 <p._i2)
retval = true;
if(_i2 >p._i2)
retval = false;
return retval;
I tried this and it seems to work fine now.... I need to do proper testing before i could say hurrahhhhhh!
Thank you Lindley!
-
November 18th, 2008, 01:59 PM
#17
Re: Operator overloading
Hmm, that doesn't seem quite right to me.
Code:
if( _first < p._first )
return true;
if(_first > p._first)
return false;
if(_i1 < p._i1)
return true;
if(_i1 > p._i1)
return false;
return _i2 <p._i2;
This is different in the sense that it will *always* make its decision on the earliest possible evidence. Your code might find _first < p._first and set retval to true, but then change its mind when it finds that _i2 > p._i2 and actually return false.
-
November 19th, 2008, 05:31 AM
#18
Re: Operator overloading
ok, if i understand you correctly, then instead of setting a variable value true or false, return it. In your code, you haven't used the ==, i believe that part of my code is right?
Code:
if( _first < p._first )
return true;
if(_first > p._first)
return false;
if(_first == p._first)
if(_i1 == p._i1)
if(_i2 == p._i2)
return false;
if(_i1 < p._i1)
return true;
if(_i1 > p._i1)
return false;
if(_i2 <p._i2)
return true;
if(_i2 >p._i2)
return false;
-
November 19th, 2008, 06:34 AM
#19
Re: Operator overloading
 Originally Posted by rachelason
ok, if i understand you correctly, then instead of setting a variable value true or false, return it. In your code, you haven't used the ==, i believe that part of my code is right?
I think you should write down on a piece of paper when given two of your objects, what exactly makes one object less than the other (not in C++, but in English). In this, you must make sure that your definition of "less than" is consistent, regardless of the two objects given to you (this is the strict weak ordering).
Then and only then should you now translate what you have on paper to C++ code. Trying to implement "less than" in C++ code when you haven't solidly come up with the definitive description of what is "less than" is not the correct approach.
Regards,
Paul McKenzie
-
November 19th, 2008, 07:15 AM
#20
Re: Operator overloading
 Originally Posted by rachelason
In your code, you haven't used the ==, i believe that part of my code is right?
Correct, but unnecessary: that case is already covered by my code. Examine it step by step assuming equal inputs to see that.
-
November 19th, 2008, 08:33 AM
#21
Re: Operator overloading
You have some "blocks"...each block is Red, Green or Blue (single monochromatic color).
1) Remove all the Red Blocks
2) Remove all the Greeen Blocks
3) Check that all the remaing blocks are Blue.
#3 is obviously redundant. The same is true for checking conditions that are guaranteed to be true by prior tests (and the associated EXITING of the return statement).
Make Sense??
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|