Thread: Overloading problem Again

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    219

    Overloading problem Again

    I am overloading += operator in this way
    Code:
    Point Point::operator+=(const Point& pt){
    	int tmp_x =this->x + pt.x;
    	int tmp_y =this->y + pt.y;
    	return Point(tmp_x, tmp_y);
    }
    and this is my Copy Constructor
    Code:
    Point::Point(const Point& pt){
    	*this = pt;
    }
    Why teh Operator Is not getting Overloaded ??
    I know making x += pt.x or this->x += pr.x Works. I wanna Know Why it doesnt Work ??

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Think for a moment what operator += does for an int variable.
    it assignes a new value to that variable.
    Does your operator change the object ( this )?
    Kurt

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Yes
    As if you wanna Do x+=y;
    It will chnage the x just like that the Object needs to be changed here.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Because the semantics are off. The semantics you implemented are those of +, not +=. += modifies the left object.
    This is the canonical form for overloading mathematical operators:
    Code:
    class Point {
      // ...
      Point &operator += (const Point &o);
    };
    
    Point &Point::operator += (const Point &o)
    {
      x += o.x;
      y += o.y;
      return *this;
    }
    
    Point operator +(const Point &lhs, const Point &rhs)
    {
      Point tmp(lhs);
      tmp += rhs;
      return tmp;
    }
    Note that the overload of + is exactly the same for every class that needs it. It's neither a member nor a friend. All it needs is a += in the class.

    As for +=, it returns a Point& for the same reason that = does: chaining.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Quote Originally Posted by CornedBee
    Code:
    Point &Point::operator += (const Point &o)
    {
      x += o.x;
      y += o.y;
      return *this;
    }
    here you are Using x += o.x;
    So you are updating the x and y DIRECTLY then is there any need of returning that this ??.
    cause The Item Thats on the left side of += Is (this) here and in x += o.x; teh First x is of the Left ones So Is there any need of return as you are Updating it Directly ??
    -----------------------------------
    I didnt Understand
    Because the semantics are off. The semantics you implemented are those of +, not +=. += modifies the left object.
    This is the canonical form for overloading mathematical operators:
    Properly. can you explain more ??
    Last edited by noobcpp; 07-01-2007 at 06:24 AM.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Your original code:
    Code:
    Point Point::operator+=(const Point& pt){
    	int tmp_x =this->x + pt.x;
    	int tmp_y =this->y + pt.y;
    	return Point(tmp_x, tmp_y);
    }
    Doesn't modify this. It just creates a new point whose components are the sum of the components of this and pt. Those are the semantics of +. The semantics of += demand that this is modified. That's what my implementation does.

    The object is then returned for chaining, as I said.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Sorry I am not so good in English.
    What does semantics maen ??

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Sorry for that. In linguistic, the semantics of a sentence are its meaning. In programming languages, therefore, the semantics of a code snippet are what it does. (As opposed to the syntax, which just says what it looks like.)

    Syntax and semantics are important concepts when studying programming languages.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    So as you are Modifying `this` Directly in your Code. Why do you need to return that ?? Isnt void Ok for that ??

  10. #10
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    It has been mentioned. You want to return a reference to the object to be able to do chaining.
    like this
    Code:
    sometype a;
    sometype b;
    b = a += 2;
    for this to work operator += has to return an object of sometype
    if you want to be able to do
    Code:
    (a+=2) = 3;
    then operator += has to return a reference

    If you don't want anything like this you return void.
    Kurt

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    But you do want it, because it is consistent with the behaviour of built-in types. Such consistency is important. If overloaded operators are inconsistent with the built-in operators (unless the problem domain is completely different) the chaos ensues.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    OK
    I've Understood it Properly
    Thanks
    a OP b OP c
    here Up to b OP C is OK
    When a cant Use the OP with <blank> Void
    Thats Why it needs Pointer or refference or Object.
    Am I right ??
    //I Meant Operator with OP

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by ZuK View Post
    It has been mentioned. You want to return a reference to the object to be able to do chaining.
    like this
    Code:
    sometype a;
    sometype b;
    b = a += 2;
    for this to work operator += has to return an object of sometype
    if you want to be able to do
    Code:
    (a+=2) = 3;
    then operator += has to return a reference

    If you don't want anything like this you return void.
    Kurt
    Code like that last example is why they should have made it standard to return a const reference.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  14. #14
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by iMalc View Post
    Code like that last example is why they should have made it standard to return a const reference.
    I just said that you can do that.
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. operator overloading slight problem
    By reddzer in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2006, 04:30 PM
  2. oops? not sure. operator overloading (possible) problem
    By w00tw00tkab00t in forum C++ Programming
    Replies: 8
    Last Post: 02-08-2006, 05:38 AM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM