Question

In the c++ standard said

A prefix unary operator shall be implemented by a non-static member function (9.3) with no parameters or a non-member function with one parameter.

But there is no some info about postfix operator overloading.

Was it helpful?

Solution 2

operator-- and operator++ are unusual because they exist in both prefix and postfix forms, so something needs to be done to differentiate between the two. As you've already noted, they decided to differentiate based on having the postfix form receive an extra (unused) parameter.

You can overload other postfix operators (e.g., operator[], operator() and operator->), but you don't have to do anything special to overload them because they only exist in postfix form.

OTHER TIPS

About postfix operators (13.5.7)

The user-defined function called operator++ implements the prefix and postfix ++ operator. If this function is a member function with no parameters, or a non-member function with one parameter of class or enumeration type, it defines the prefix increment operator ++ for objects of that type. If the function is a member function with one parameter (which shall be of type int) or a non-member function with two parameters (the second of which shall be of type int), it defines the postfix increment operator ++ for objects of that type. When the postfix increment is called as a result of using the ++ operator, the int argument will have value zero.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top