operator overloading

This is a discussion on operator overloading within the C++ Programming forums, part of the General Programming Boards category; Hello I am new to operator overloading. I have written this code but I don't undrestand why the ob1-- command ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    177

    operator overloading

    Hello

    I am new to operator overloading.
    I have written this code but I don't undrestand why the ob1-- command doesn't work as it's expected.

    Code:
    # include<iostream>
    
    using namespace std;
    
    class loc
    { 
    	int lenght;
    	int width;
        public:
    		loc(){};
    		loc(int lg, int lt)	{ lenght=lg;  width=lt;	}
    		void show()	{ cout<<lenght<<"  "<<width<<'\n'; }
    		
    		loc operator*(loc);		
    		
    		loc operator--();
    		loc operator--(int);
    
    		loc operator+=(loc );
    
    		loc& operator=(loc & );
    
    		friend loc operator+(loc ,loc );
    };
    
    
    loc loc :: operator*(loc op2)
    {
    	loc temp;
    	temp.lenght = lenght * op2.lenght;
    	temp.width = width * op2.width;
    	return (temp);
    }
    
    loc loc::operator--()
    {
    	lenght--;
    	width--;
    	return (*this);
    }
    
    loc loc::operator--(int)
    {
    	loc temp=*this;
    	return (temp);
    }
    
    loc loc::operator+=(loc op2)
    {
    	lenght=lenght+op2.lenght;
    	width=width+op2.width;
    	return (*this);
    }
    
    loc& loc::operator =(loc & op2)
    {
    	lenght=op2.lenght;
    	width=op2.width;
    	return (*this);
    }
    
    loc operator+(loc op1,loc op2)
    {
    	loc temp;
    	temp.lenght=op1.lenght+op2.lenght;
    	temp.width=op1.width+op2.width;
    	return(temp);
    }
    
    
    int main()
    {
    	loc ob1(10,20),ob2(3,5),ob3;
    	cout<<"ob1: ";
    	ob1.show();
    
    	cout<<"ob2: ";
    	ob2.show();
    
    	ob3=ob1;
    	cout<<"ob3: ";
    	ob3.show();
    
    	ob3+=ob1;
    	cout<<"ob3 += ob1 : ";
    	ob3.show();
    
    	ob1=ob1+ob2;
    	cout<<"ob1 + ob2 : ";
    	ob1.show();
    
    	loc ob4=ob1--;
    	cout<<"ob4= ";
    	ob4.show();
    	cout<<"ob1= ";
    	ob1.show();
    
    	loc ob5=--ob1;
    	cout<<"ob5= ";
    	ob5.show();
    	cout<<"ob1= ";
    	ob1.show();
    
        cin.get();
    	return(0);
    }
    output:

    Code:
    ob1: 10  20
    ob2: 3  5
    ob3: 10  20
    ob3 += ob1 : 20  40
    ob1 + ob2 : 13  25
    ob4= 13  25
    ob1= 13  25             --------------->why isn't it  12  24
    ob5= 12  24
    ob1= 12  24
    also
    could you please tell me the difference between the following. I have 2 c++ books and read the operator overloading parts/examples but I still dont get the necessity of 'const ... &' for the '+' operator

    Code:
    friend loc operator+(loc op1,loc op2);
    friend loc operator+(const loc& op1,const loc& loc op2);
    I though, as [ const class_name & ] is for the "copy constructor",it should be used for overloading '=' not the others.

    thank you
    Arian

  2. #2
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Code:
    
    loc loc::operator--(int)
    {
    	loc temp=*this;
    	return (temp);
    }


    Quick attempt at this...
    you return the object but do not perform any operation it?

    Const means you do not intend to modify the object you passed as a parameter. If you intend to, then there is no need for the const specifier
    EDIT:
    you probably want to do this

    Code:
    
       loc loc::operator--(int num)
      {
    	loc temp=*this;
    
           temp.x = temp.x - num ; //here
    	return (temp);
         }
    Last edited by Eman; 12-22-2010 at 09:34 AM.
    You ended that sentence with a preposition...Bastard!

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    177
    How should I write the code which shows the difefrence between --a and a-- ?

  4. #4
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    To be honest I don't know. i haven't tried this before.

    But we know that
    num-- isn't used immediately.
    So maybe do this

    Code:
    num= num -1
    and when you leave that statement, to a new instruction, find a way to update num to its new value
    EDIT:

    you might want to store the result of num-- in a temp value, and update that value into num later or something like that..
    Last edited by Eman; 12-22-2010 at 09:49 AM.
    You ended that sentence with a preposition...Bastard!

  5. #5
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    How should I write the code which shows the difefrence between --a and a-- ?
    This:
    Code:
    loc loc::operator--(int num)
      {
    	loc temp=*this;
    
           temp.x = temp.x - num ; //here
    	return (temp);
         }
    Is the postfix "a--".
    The int argument is used to distinguish between the two - however you don't specify the argument when writing a--, and it isn't used in the function itself. It's only a placeholder.
    Consider this post signed

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So to do a post-decrement, you need to copy the current variable into a scratch variable, do the decrementing on the real variable, then return the scratch (non-decremented) variable. I believe it is fairly common to call pre-decrement inside the post-decrement function. It's also bad form to actually use the num parameter in the post-decrement function.

  7. #7
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Oh it should be

    Code:
    loc loc::operator--(int)
      {
    	loc temp=*this;
    
           temp.x-- //here
    	return (temp);
         }
    I just tried to implement --a
    failed.
    Last edited by Eman; 12-22-2010 at 10:12 AM.
    You ended that sentence with a preposition...Bastard!

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Eman View Post
    Oh it should be

    Code:
    loc loc::operator--(int)
      {
    	loc temp=*this;
    
           temp.x-- //here
    	return (temp);
         }
    I just tried to implement --a
    failed.
    I would guess you want
    Code:
    loc loc::operator--(int)
    {
        loc temp = *this;
        --(*this);
        return temp;
    }
    in that you want to decrement the current object, and not your scratch variable. This assumes you have the pre-decrement set up to do what you want.

  9. #9
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    what is a scratch variable, like a temp? google doesn't have anything on that.
    And how would it know the difference between this operator call a-- and this call --a ?
    You ended that sentence with a preposition...Bastard!

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It's true that the Google search is a bit contaminated by MIT's Scratch the Cat. But still, heaven forbid you should have to look at link number five or link number six or ....

    And the compiler can see the difference between
    Code:
    operator--()
    and
    Code:
    operator--(int num)
    even if you can't.

  11. #11
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by tabstop View Post

    And the compiler can see the difference between
    Code:
    operator--()

    I get it if that works for a--
    but like the OP asked, if we wanted to do a pre decrement on the object

    i can't do this --operator()
    so how would the compiler tell
    to use operator--() for a--
    and operator--() for --a?

    how do we work around this?
    You ended that sentence with a preposition...Bastard!

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    Quote Originally Posted by Eman View Post
    I get it if that works for a--
    but like the OP asked, if we wanted to do a pre decrement on the object

    i can't do this --operator()
    so how would the compiler tell
    to use operator--() for a--
    and operator--() for --a?

    how do we work around this?
    The correct function signatures for all operator overloads are shown here.
    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"

  13. #13
    Registered User
    Join Date
    Nov 2003
    Posts
    177
    I found this

    Code:
    loc loc::operator--(int)
    {
    	loc temp=*this;
                    lenght--;
    	width--;
    	return (temp);
    }
    will solve the problem.

    "this" points to the current obj.
    Before changing anything, copy it into temp.
    by
    lenght--;
    width--;
    the object will be changed but the previous value is returned by return(temp).

    So it shows the difference between --a and a--


    I still dont know how the compiler recognizes these 2 functions by using the "int" parameter which is not even named!!

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by arian View Post
    I still dont know how the compiler recognizes these 2 functions by using the "int" parameter which is not even named!!
    Really? Do you not see the difference between "nothing" and "something"?

  15. #15
    Registered User
    Join Date
    Nov 2003
    Posts
    177
    I have another question too.

    I am confused:
    I got 2 example functions which returns "*this".
    one of them is like:
    Code:
    class_name &  fun_name (class_name);
    the other one is:
    Code:
    class_name fun_name (class_name);
    if it is "return(*this)" in both of them , how come the return type is different?!

Page 1 of 2 12 LastLast
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