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
}

Recommended Answers

All 3 Replies

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.