2. Unary post #

Created Sunday 12 January 2020

  1. Both pre and post use the same symbol, how to differentiate pre and post in code?
  2. Write data_type in parentheses. This indicates that the operator is a post(i.e appears on the right side of the operand).

Example

class A
{
    public:
        int a;
        A(int a)
        {
            this->a = a;
        }

        void operator++(int) // ++ as postfix
        {
            a++;
            cout << "post\n";
            return a;
        }

        A& operator++() // ++ as prefix, nesting is present return reference
        {
            cout << "pre\n";
            ++a;
            return a;
        }
};