Overloading ++ Operator

This is a discussion on Overloading ++ Operator within the C++ Programming forums, part of the General Programming Boards category; this is a pointer, it has the type Point*. If you wanted to return a pointer (Point*) you could return ...

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    this is a pointer, it has the type Point*. If you wanted to return a pointer (Point*) you could return this (but you shouldn't here). If you want to return a reference (Point&), you must dereference the pointer with *this like you did. If you want to return an object (Point), you must also dereference the pointer with *this, and then a copy is automatically made and returned.

  2. #17
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    No I must use Point& else Its Firing wired Linker errors.

  3. #18
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    This is what I've done For Postfix
    Why its firing Errors ??
    Code:
    Point& operator++(int);
    Code:
    Point& Point::operator++(int m=0){//What Argument would I use here what int ??
    	x++;
    	y++;
    	return *this;
    }
    And I am using it in this way
    Code:
    Point y = pt++;

  4. #19
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    If you are getting errors, post the errors exactly as they appear.

    In your post (#15) above, your code was different between the two versions. In the Point& version you returned *this. In the Point version you returned this. Both should return *this.

    If you are getting linker errors, that means you are doing something else wrong, but I can't tell what without looking at the code and the errors.

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,594
    No I must use Point& else Its Firing wired Linker errors.
    That is probably due to an inconsistency between your class declaration and its implementation.

    Why its firing Errors ??
    Look at CornedBee's example. You could implement your post-increment operator++ as:
    Code:
    const Point Point::operator++(int)
    {
        Point temp(*this);
        ++*this;
        return temp;
    }
    If it does not work, then either your copy constructor or pre-increment operator++ is incorrect.

    What Argument would I use here what int ??
    Anything if you are calling the operator by its name as a function, nothing if you are using it as an operator. For example:
    Code:
    // Assume pt is a Point object.
    pt++; // Post-increment operator used as an operator.
    pt.operator++(123); // Post-increment operator called by name as a function.
    // Note that 123 is just an example, you can use any other int values too.
    Note also that since it is post-increment (or postfix notation, if you prefer), your example usage will result in y being a copy of pt, and pt being post-incremented. As such, make sure that you are checking for the right thing.
    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

  6. #21
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    OK now this is my Compleate Code http://phpfi.com/246304
    Code:
    const Point operator++(int);
    Code:
    const Point Point::operator++(int){
    	Point tmp(*this);
    	++*this;
    	return tmp;
    }
    And I am getting this error
    Code:
    /tmp/ccF6kXes.o: In function `Point::operator++(int)':
    op_overload.cpp:(.text+0xd9): undefined reference to `Point::Point(Point const&)'
    collect2: ld returned 1 exit status

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,594
    You did not implement the copy constructor for Point. As a quick fix, you can just remove it and let the compiler generate it for you.
    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

  8. #23
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Oh! I see
    This is teh declearation of My Copy Constructor
    Code:
    Point(const Point&);//Copy Constructor
    Would I Implement it in this way ??
    Code:
    Point::Point(const Point& pt){
    this = *pt;
    }
    Just this ??Or I need to Specifi Any Return Type ??

  9. #24
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    The best way would be to remove it because the compiler generated one works correctly. If you implement it yourself, then you have to make sure it stays updated and correct, which can be a source of hard to find bugs.

    If you were to implement it yourself, you would initialize each member variable with the member variable of the other object.

    >> Or I need to Specifi Any Return Type ??
    Like all other constructors, there is no return type.

  10. #25
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    I also need to learn Copy constructor
    Can you give me an example ??

  11. #26
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    OK I've made this
    default constructor
    Code:
    Point::Point(const Point& pt){
    	*this = pt;
    }
    And here is My Compleate code now http://phpfi.com/246309
    Now When I run this Programm Post fix ++ works same as Prefix ++ Operatotr there is No Difference
    here is teh Output of the Program
    Code:
    describing pt   x = 4   y = 10
    describing pt after Pre Incrementing    x = 5   y = 11
    describing y    x = 6   y = 12
    describing pt after Post Increment      x = 6   y = 12
    destructing
    destructing

  12. #27
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by noobcpp View Post
    Now When I run this Programm Post fix ++ works same as Prefix ++ Operatotr there is No Difference
    Shure there is a difference. You're just testing it the wrong way
    try like this
    Code:
    int main(){
    	Point pt(4, 10);
    	pt.desc("pt initialized          ");
    	Point px = ++pt;
    	px.desc("px returned from pre inc");
    	pt.desc("pt after Pre Incrementing");
    	Point py = pt++;
    	py.desc("py returned from post inc");
    	pt.desc("pt after Post Increment");
    
    }
    Kurt

  13. #28
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Wow Its Working Thanks.

  14. #29
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    That's not a really sensible way to implement the copy constructor. Here's a good one:
    Code:
    Point::Point(const Point &o) : x(o.x), y(o.y) {}
    But that's still not smart. Daved is right: remove it, because the compiler-generated one does the right thing. And because you're violating the rule of three. (Just a guideline, but an important one.)

    Like with pointers, you need to learn not only how, but also when and why. You implement copy constructors when you need them. And, by the rule of three, you also implement the copy assignment operator and the destructor, because you will need them too in such a situation.

    If you want an example where you need these, implement a resizing array similar to std::vector.
    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

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 01:14 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 07:38 AM
  4. C++ Operator Overloading help
    By Bartosz in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2005, 12:55 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM

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