CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 21 of 21
  1. #16
    Join Date
    Oct 2006
    Posts
    128

    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!

  2. #17
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  3. #18
    Join Date
    Oct 2006
    Posts
    128

    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;

  4. #19
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Operator overloading

    Quote Originally Posted by rachelason View Post
    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

  5. #20
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Operator overloading

    Quote Originally Posted by rachelason View Post
    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.

  6. #21
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    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

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured