As we know to differentiate between pre-increment & post-increment operator function, we use a dummy argument in post-increment operator function. But how the compiler INTERNALLY differentiate between these two functions as we know in function overloading, compiler differentiate multiple functions(of same name) by number of arguments passed(& the arguments is received by the function), but here we don't pass any argument while calling, but in argument of function definition we declare 'int'.

class Integer
{
    int x;
public:
    void setData(int a)
    { x = a; }
    void showData()
    { cout<<"x="<<x; }
    Integer operator++()      // Pre increment
    {
       Integer i;
       i.x = ++x;
       return i;
    }

    Integer operator++(int)      // Post increment
    {
       Integer i;
       i.x = x++;
       return i;
    }
};

void main()
{
   Integer i1,i2;
   i1.setData(3);
   i1.showData();
   i2 = ++i1;     // Calls Pre-increment operator function
   i1.showData();
   i2.showData();
   i2 = i1++;     // Calls Post-increment operator function
   i1.showData();
   i2.showData();
}


Another question, why i2 = i1++ calls post-increment function why not pre-increment one. Since we are not passing any value, how compiler calls only the postfix function. Is it predefined that 'dummy argument function' is used for post-fix function call only?
Also, can we pass other 'float', 'double' or other datatypes as dummy argument instead of only 'int'?
Only one argument is used as dummy or more than one?

2

There are 2 best solutions below

0
On

For starters the pre-increment operator should be declared and defined like

Integer & operator++()      // Pre increment
{
   ++x;
   return *this;
}

In these statements

i2 = ++i1;
i2 = i1++; 

there is explicitly visible which operator is called that is whether the unary increment operator

i2 = ++i1;

or the postfix increment operator

i2 = i1++; 

So the compiler does not have a problem to parse these statements and to select the appropriate function.

As for the last your question then for example according to the C++ 14 Standard (13.5.7 Increment and decrement)

1 The user-defined function called operator++ implements the prefix and postfix ++ operator. If this function is a non-static member function with no parameters, or a non-member function with one parameter, it defines the prefix increment operator ++ for objects of that type. If the function is a non-static 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.1

To distinguish the unary increment operator from the postfix increment operator when they are called explicitly you need to specify for the postfix increment operator an argument equal to 0.

For example

i1,operator ++();      // the unary increment operator
i1.operator ++( 0 ) ;  // the postfix increment operator 
0
On

Not sure if I understand the question. The compiler can distinguish them because they are two different operators. x++ is different from ++x.

Maybe it helps to consider that custom operators are just syntactic sugar for calling special methods that you can overload. For example if you have:

#include <iostream>

struct foo {
    void operator++(int) {
        std::cout << "post";
    }
    void operator++(){
        std::cout << "pre";
    }
};

Then you can call the methods via x++ or ++x or via the lengthy:

int main()
{
    foo f;
    f.operator++(0);
    f.operator++();
}

And this is no different from the compiler deciding what method to call here:

void bar(int);
void bar();
bar();
bar(0);