I’m overloading the ++ prefix operator using a member function. Here’s the prototype:
Test &operator++();
But my doubt comes when I use it for my object like below:
Test t;
++t;
As far I have learned that for any operator overloaded by a member function there should be a object of that same class on left side of that operator. But when I am calling this ++ prefix overloaded operator, I don’t need any object of Test class on the left side.
Why?
That would hold for any binary operator. Pre- and post-incrementations, as well as the dereference operator (
*obj) are unary operators. They have a single argument (either a function parameter or implied “this” parameter, depending on how you overload the operator) and for overloaded operators this only argument must be of a class type.Unary operators don’t have a “left” and “right” sides (operands), they only have one operand.
Remember that in your case:
means just:
So – in some twisted thinking –
tis indeed on the left side. 🙂