0
votes

I am reading the following article from IBM: http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr323.htm,

it mentioned as: You overload a unary operator with either a nonstatic member function that has no parameters, or a nonmember function that has one parameter. And then give a example how to overload it, but why we can overload nonstatic mumber function has no parameters, but nonmember function that has one parameter?

2
The "one parameter" is provided for you if it is a non-static member, namely *this. A non member has to have something to work on, so the parameter is required.WhozCraig

2 Answers

0
votes

Because the nonstatic member function operates on the instance which it automatically has access to through the this pointer.

The nonmember function needs a parameter to be able to do something to the instance.

0
votes

Unary operator needs a object to perform an action on. If you have non-member operator, you need to have one argument - and that will be the target object. If you have a member operator, you don't need to specify an argument - it will be passed implicitly as this pointer - so it will perform an action on a current object that this operator is called for.

Remember - for all non-static member functions, compiler is always passing this pointer as a invisible parameter.