operator Overloading

This is a discussion on operator Overloading within the C++ Programming forums, part of the General Programming Boards category; I am facing Problem while overloading the += Operator I've posted My Code here http://phpfi.com/245888 So that you can read ...

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

    operator Overloading

    I am facing Problem while overloading the += Operator
    I've posted My Code here http://phpfi.com/245888 So that you can read it easily. The problems are Commented Out there with
    Code:
    //Here is the Overloading Problem
    Here I am re describing it.
    This is the Prototype
    Code:
    		Point operator +=(int);//Overloading += Operator
    This is the operator Function
    Code:
    Point Point::operator+=(int inc){
    	this->x += inc;
    	this->y += inc;
    	return *this;
    }
    And this is how I am using this
    Code:
    pt2+=5;//here is the problem
    The Problem is the Output
    Code:
    Describing Class :              pt1             x = 15  y = 10
    Describing Class : pt2 before Copying           x = 4   y = 8
    Describing Class : pt2 after Copying            x = 15  y = 10
    Describing Class : pt2 after Increment          x = 19  y = 0
    It shouldn't Be 19 and 0
    Whatever I Do with it Its always 19 and 0 Why ??
    How can I solve it ??
    I an using g++ on Linux

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,954
    Why not post the smallest and simplest compilable code that demonstrates the error? So far it looks okay, except that operator+= typically returns a reference. Perhaps the mistake lies in the function that prints the member variables, for example.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> pt2+=5;

    Your problem is that pt2 is a pointer, so you are doing a pointer increment, you are not calling your operator +=.

    Don't make p1 and p2 pointers, there's no reason to in this example (and you have memory leaks as well). Just make them local objects:
    Code:
    Point pt1(15,10);
    Point pt2(4,8);
    If you need them to be pointers for some reason, then to call the operator += you would do this:
    Code:
    (*p2) += 5;

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    You're not calling the increment operator that you've overloaded; you're just incrementing the pointer ahead 5 times. By the same token, you're not calling the assignment operator that you've overloaded; you're just doing pointer assignment. Try this:
    Code:
    int main(){
    	Point* pt1 = new Point(15, 10);
    	pt1->desc("		pt1");
    	Point *pt2 = new Point(4, 8);
    	pt2->desc("pt2 before Copying");
    	(*pt2)=(*pt1);//Works fine
    	pt2->desc("pt2 after Copying");
    	(*pt2)+=5;//here is the problem
    	pt2->desc("pt2 after Increment");
    	return 0;
    }
    You'll see that your increment operator works fine, while your assignment operator could use some work.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    	pt2+=5;//here is the problem
    exactly. since pt2 is a pointer to Point this doesn't call the += operator.
    It just advances the pointer by 5 * sizeof( Point ) and then you print an uninitialized Point object.
    You're lucky that this doesn't crash.
    Works ok if you use objects like this
    Code:
    int main(){
    	Point p(15,10);
    	p+=2;
    	p.desc("");
    	return 0;
    }
    BTW: this is a rather unusual operator =
    Code:
    Point* Point::operator=(const Point& pt)
    Kurt

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,954
    Oops, my bad. I either did not see the link to the pastebin, or it was edited in quickly
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Thanks To all.
    I didnt Understand this.
    Quote Originally Posted by Zuk
    It just advances the pointer by 5 * sizeof( Point ) and then you print an uninitialized Point object.
    You're lucky that this doesn't crash.
    What happens phisically
    does it allocates more memmory for that Pointer ??
    or It moves the Point to the Previous Address to a new Address ??
    Why the x and y members of the pt2 Object Gets Changed ??
    Describing Class : pt2 after Increment x = 19 y = 0

  8. #8
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    No memory is allocated. You are printing an object that is physically 5 objects away ftrom the one that you allocated. It holds random data.
    Kurt

  9. #9
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Another More Question
    Whats teh Defference between
    *pt
    and (*pt)
    ??

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,954
    No difference. There is only a difference in conjunction with other operators, e.g. (*pt).member is equivalent to pt->member but not *pt.member
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    K thank you all very much

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21