Thread: operator overloading

  1. #1
    cereal killer dP munky's Avatar
    Join Date
    Nov 2002
    Posts
    655

    operator overloading

    i have another assignment (the last thank goodness) but here it is...

    i have to write the definitions for the functions in a class....these happen to deal w/funciton overloading, i had a similar assignment earlier....am i doing this right? //the first one imparticular
    Code:
    		// prefix ++ operator
    		Point& operator++(pt)
    		{
    			x_ = pt.x_++;
    			y_ = pt.y_++;
    			z_ = pt.z_++;
    		}
    
    		const Point operator++(int);		// postfix ++ operator
    
    		Point& operator-(); 				// prefix -- operator
    		const Point operator-(int);		// postfix -- operator
    guns dont kill people, abortion clinics kill people.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    nope. remember the int passed is a dummy arg to differentiate post and pre.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    cereal killer dP munky's Avatar
    Join Date
    Nov 2002
    Posts
    655
    excuse me for being retarded but....huh?
    guns dont kill people, abortion clinics kill people.

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    The integer passed as an argument has no use to you. It just tells the compiler whether the operator is postfix or prefix.

  5. #5
    cereal killer dP munky's Avatar
    Join Date
    Nov 2002
    Posts
    655
    so then how do i make it usefull to me? remove it?
    guns dont kill people, abortion clinics kill people.

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    ok here we go again post and pre for dummies....
    Code:
    class plonker
    {
       private: 
         int wally;
       public:
         plonker() : wally(0){}
         plonker(int w) : wally(w){}
         plonker& operator ++() 
         {
             ++wally;
             return *this;
         }
         const plonker operator ++(int)
         {
            int temp = wally;
            ++(*this); // implement post in terms of pre for symettry
            return temp;
         }
    };
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Check this out.

  8. #8
    cereal killer dP munky's Avatar
    Join Date
    Nov 2002
    Posts
    655
    getting warmer???

    Code:
    		// prefix ++ operator
    		Point& operator++()
    		{
    			++x_;
    			++y_;
    			++z_;
    			return *this;
    		}
    
    		// postfix ++ operator
    		const Point operator++(int)
    		{
    			int temp = *this;
    			++(*this);
    
    			return temp;
    
    		}
    Last edited by dP munky; 04-17-2003 at 10:17 PM.
    guns dont kill people, abortion clinics kill people.

  9. #9
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    1) fine
    2)crap. try again
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  10. #10
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    look im off to bed so one last piece of advice.......

    Use the constructor......
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  11. #11
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    >> int temp = *this;


    think about what 'this' is and what your assigning it to. 'this' is not an int.

  12. #12
    cereal killer dP munky's Avatar
    Join Date
    Nov 2002
    Posts
    655
    this should be my 3 ints...right? this is a pointer to x_ y_ and z_ or am i misunderstanding that?

    im looking at this web site and it says to just do

    ++(*this);

    am i doing to mucH?
    Last edited by dP munky; 04-17-2003 at 10:50 PM.
    guns dont kill people, abortion clinics kill people.

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    The problem is here:

    int temp = *this;

    this is a pointer, and like all pointers it points to one thing, not three things. What does this point to? this is a pointer to the object you are calling the function on, so *this is the object. The object's type is the class name not int.

    "the int passed is a dummy arg to differentiate post and pre."

    Try defining post and prefix ++ operators with out using the dummy int:

    operator++()

    operator++()

    Those are exactly the same, so the compiler can't tell the difference. You need int as a dummy argument to differentiate the two:

    operator++()
    operator++(int)

    Since there is no requirement that you have to use an argument that's passed to your function, ignore it in your function definition.
    Last edited by 7stud; 04-18-2003 at 01:45 AM.

  14. #14
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    well did you get it right?

    Code:
    const Point operator++(int)
    {
       Point temp(*this); // call copy con to make copy of current object
       ++(*this);
       return temp;
    }
    Its that simple.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  15. #15
    cereal killer dP munky's Avatar
    Join Date
    Nov 2002
    Posts
    655
    i had

    Point temp = *this;

    but i went to bed shortly after that, i had another assignment due at midnight i was rushin to get done
    guns dont kill people, abortion clinics kill people.

Popular pages Recent additions subscribe to a feed