3

Class declaration:

class unaryOperators 
{
    public:
        int i;

        unaryOperators (int tempI = 0)
        {
            i = tempI;
        }

        unaryOperators operator++ (int);
        unaryOperators operator++ ();
};

Does this global definition correspond to postfix or prefix version of the overloaded operator++? Why?

unaryOperators operator++ (unaryOperators &one)
{   
    return one; 
}
2
  • I see. By the way, I didn't down-vote. Hm, I'm surprised, actually. I removed the declarations inside the class, added log inside unaryOperators operator++ (unaryOperators &one), created object unaryOperators a and tried a++; ++a; and both printed the added log!. Interesting Jan 6, 2012 at 7:11
  • 1
    @AnishaKaul : +1 for the way you have presented the question :) Jan 21, 2012 at 8:46

6 Answers 6

7
unaryOperators& operator++ (unaryOperators &one)
              ^^

is the non-member prefix unary increment operator.

The non-member postfix unary increment operator takes an additional int as an policy enforcing parameter.

unaryOperators operator++ (unaryOperators &one, int)

Reference:

C++03 Standard 13.5.7 Increment and decrement [over.inc]

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.125)

[Example:
class X {
   public:
      X& operator++(); // prefix ++a
      X operator++(int); // postfix a++
};
class Y { };
Y& operator++(Y&); // prefix ++b
Y operator++(Y&, int); // postfix b++

void f(X a, Y b) {
++a; // a.operator++();
a++; // a.operator++(0);
++b; // operator++(b);
b++; // operator++(b, 0);
a.operator++(); // explicit call: like ++a;
a.operator++(0); // explicit call: like a++;
operator++(b); //explicit call: like ++b;
operator++(b, 0); // explicit call: like b++;
}
—end example]
1
  • @AnishaKaul: Updated the reference from the Standard for completeness.
    – Alok Save
    Jan 6, 2012 at 9:02
3

I think this will help you.

1
  • It would be very helpful to include a summary of the answer here, rather than solely posting a link to some material on the web that may/may not be available to future readers.
    – razlebe
    Jan 6, 2012 at 7:49
2

Every operator (that can be overloaded as a free function) takes one more argument when overloaded as a free function. The first argument corresponds to *this when overloaded as a member function.

bool AsMember::operator!() const;
bool operator!(const AsFreeFunction&);

bool AsMember::operator==(const AsMember& rhv) const;
bool operator==(const AsFreeFunction& lhv, const AsFreeFunction& rhv);

etc.

Increment operator is no exception to this.

0
1

The free function is prefix as it lacks an int parameter.

Helpful guide for operator signatures.

5
  • Yeah, but the c++faq says that the prefix version doesn't take ANY arguments, but in this case one argument is supplied. Jan 6, 2012 at 7:08
  • and also, writing the above function inside the class gives the error: error: postfix ‘unaryOperators unaryOperators::operator++(const unaryOperators&)’ must take ‘int’ as its argument which means that compiler doesn't take it as prefix. Jan 6, 2012 at 7:09
  • The preincrement operator doesn't take any args when defined in the class. When defined outside the class, it takes one argument -- a reference to the thing to increment.
    – cHao
    Jan 6, 2012 at 7:10
  • @AnishaKaul The in-class prefix doesn't have any parameters other than the hidden this parameter. You are getting that error because prefix should return a reference unaryOperators&.
    – Pubby
    Jan 6, 2012 at 7:12
  • 1
    No, that error means that when implemented as a class member, it must take either no arguments or an int argument. The language doesn't care what you return (could be even void or some other type altogether).
    – UncleBens
    Jan 6, 2012 at 7:37
1

The canonical version of preincrement is:

T &operator++(T &)

That is, return the operand by reference. Postincrement takes an unused int, so the global operator++ you defined is the preincrement operator.

2
  • MSN see my comments on @Pubby 's answer. Jan 6, 2012 at 7:10
  • no it doesn't take a T& parameter when it is a class member because that parameter is implied. However here you are defining a free function so there is no implicit "this".
    – CashCow
    Jan 6, 2012 at 7:29
1

Global overloaded operator++ functions expect the explicit specification of all the arguments, so if the overloaded operator ++ is postfix, we are supposed to add one default int argument (to distinguish postfix version from prefix) in addition to the prerequisite one (which determines the type on which the function needs to be applied).

unaryOperators operator++ (unaryOperators &one, int dummy)
{   
    return one; 
}

In the case of prefix global overloaded operator++ functions, the only argument we need to specify is the prerequisite one (which determines the type on which the function needs to be applied).

unaryOperators operator++ (unaryOperators &one)
{   
    return one; 
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.