Here the value of int x is always zero. I don't know if there is any case where it is non-zero. If so pls reply.

int OperOverld::operator--(int x)
    cout<<x<<endl; //always zero
    //some code goes here
    //atlast return some int
6 Years
Discussion Span
Last Post by Narue

It's simply a way to distinguish between the prefix and postfix overloads. If the two didn't differ in the parameter list, they would be ambiguous.


ok thanks, one more doubt regarding the same.
The ++ operator is overloaded as shown in the above post. I tried this. And it worked

int j = obj++;

But later i thought of overloading + operator instead of ++ to do the same job. Now the below line gave compiler error.

int j = obj2+;

But this worked after I added another integer after +

int j = obj2+100;

I verified 100 is not going into the overloaded operator function. The function was simply returning a int which could have been assigned to j. It didn't happen. But when another constant is there on the right side it worked. Why is it so?


operator+ is still a binary operator, whether you use the other operand or not. You could overload the unary operator+, but it's a prefix operator.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.