0

Is there any difference between overloading unary and binary operator.

I have doubt with following codes:

overloading binary operator:

 #include<iostream>

 using namespace std;

 class ex
 {
     int i;
     int j;
 public:
     ex operator+(ex ob)
     {
       ex temp;

       temp.i=this->i+ob.i;
       temp.j=this->j+ob.j;

       return temp;
    }
    ex& operator=(ex ob)
    {
       this->i=ob.i;
       this->j=ob.j;

       return *this;
    }
    void setij(int x,int y)
    {
        this->i=x;
        this->j=y;
    }

    int geti()
    {
       return i;
    }

    int getj()
    {
        return j;
    }
};



int main()
{
    ex ob1,ob2,ob;

    ob1.setij(1,1);
    ob2.setij(1,1);

    ob=ob1+ob2;   //ob1 is passed through this pointer

    cout<<"i="<<ob.geti()<<"\nj="<<ob.getj();
}

overloading unary operator:

    #include<iostream>

    using namespace std;


    class ex
    {
        int i,j;
     public:
        ex(){}
        ex &operator++();
        void setij(int x,int y);
        int geti();
        int getj();
    };

   ex &ex::operator++()
   {
       this->i++;
       this->j++;
       return *this;
   }

   void ex::setij(int x,int y)
   {
       i=x;
       j=y;
   }

   int ex::geti()
   {
      return i;
   }

   int ex::getj()
   {
      return j;
   }

   int main()
   {
      ex ob;
      ob.setij(1,1);
      cout<<"i="<<ob.geti()<<"\nj="<<ob.getj();

      ++ob;  //ob is passed through this pointer

      cout<<"\nAfter ++\n";
      cout<<"i="<<ob.geti()<<"\nj="<<ob.getj();
   }

MY question is:

1)in binary operator the this pointer passed to function is left of the operator.

  ob1+ob2; //ob1 is passed through this pointer      

2)in unary operator the this pointer to function is right of the operator.

   ++ob; //ob is passed through this pointer

3)Why int is put in postfix increment operator like ex &ex::operator++(int)

Why the this pointer passed to function vary in left and right?

output for first code :

 i=2
 j=2
 Process returned 0 (0x0)   execution time : 0.417 s
 Press any key to continue.

output for second code :

i=1
j=1
After ++
i=2
j=2
Process returned 0 (0x0)   execution time : 0.424 s
Press any key to continue.
srilakshmikanthanp
  • 2,231
  • 1
  • 8
  • 25
  • 3
    Because that's the syntax of C++. There is also `ex ex::operator++(int)` for `ob++` – Thomas Sablik Dec 11 '19 at 05:53
  • NB Your binary operator overload should accept its argument by reference, preferably by const reference. – user207421 Dec 11 '19 at 05:55
  • 1
    Does this answer your question? [overloading postfix and prefix operators](https://stackoverflow.com/questions/7740350/overloading-postfix-and-prefix-operators) – TruthSeeker Dec 11 '19 at 06:08
  • 1
    The `int` parameter for the postfix operators is a hack (some would call it "a clever workaround") whose only purpose is to distinguish between prefix and postfix. – molbdnilo Dec 11 '19 at 08:48

1 Answers1

1

this is always the first parameter, no matter where the operator goes.

Caleth
  • 52,200
  • 2
  • 44
  • 75