13

As I know here is the way to overload the post-increment operator:

const MyClass& MyClass::operator++(int);

Why does it have int as an argument?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Nayana Adassuriya
  • 23,596
  • 30
  • 104
  • 147
  • 5
    From [this answer](http://stackoverflow.com/a/4421719/499359) *The unary increment and decrement operators come in both prefix and postfix flavor. To tell one from the other, the postfix variants take an additional **dummy int argument***. – PaperBirdMaster Oct 05 '12 at 06:08
  • possible duplicate of [Why does the postfix increment operator take a dummy parameter?](http://stackoverflow.com/questions/3574831/why-does-the-postfix-increment-operator-take-a-dummy-parameter) – Ciro Santilli OurBigBook.com Jun 15 '15 at 21:57

2 Answers2

18

D&E, §11.5.3:

I conisdered the obvious solution, adding the keywords prefix and postfix to C++ [ ... ] However I received the usual howl of outrage from people who dislike new keywords. Several alternatives that did not involve new keywords were suggested. For example:

class Ptr_to_X {
    X ++operator(); // prefix ++
    X operator++(); // postfix ++
};

or

class Ptr_to_X {
    X& operator++(); // postfix because it 
                     // returns a reference
    X operator++();  // prefix because it
                     // doesn't return a reference
};

I considered the former too cute and the latter too subtle. Finally I settled on:

class Ptr_to_X {
    X operator++();     // prefix: no argument
    X operator++(int);  // postfix: because of the argument
};

This may be too cute and too subtle, but it works, requires no new syntax, and has a logic to the madness. Other unary operators are prefix and take no arguments when defined as member functions. The "odd" and unused dummy int argument is used to indicate the odd postfix operators. In other words, in the postfix case, ++ comes between the first (real) operand and the second (dummy) argument and is thus postfix.

These explanations are needed because the mechanism is unique, and therefore a bit of a wart. Given a choice I would probably have introduced the prefix and postfix keywords, but that didn't appear feasible at the time.

Anton Kesy
  • 119
  • 7
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • 1
    thank you, I'm quit confuse. How to call a function without passing parameters, but the same functions, signature has parameters. – Nayana Adassuriya Oct 05 '12 at 06:30
  • 4
    @NayanaAdassuriya: Simple: write `x++`, and that's the function that gets called. If you insist on calling it as `x.operator++`, then I believe you have to pass a parameter (which will be ignored): `x.operator++(1);` – Jerry Coffin Oct 05 '12 at 06:32
3

This is to distinguish between prefix increment and postfix increment operators.

For completeness, this is set out in §13.5.7 in both the C++03 and the C++11 standards.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • @NayanaAdassuriya I believe it is just a convention. – juanchopanza Oct 05 '12 at 06:04
  • Thanks juanchopanza:is this mentions in C++ standers? – Nayana Adassuriya Oct 05 '12 at 06:09
  • @NayanaAdassuriya, *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.* It says right after that the argument is 0. – chris Oct 05 '12 at 06:10
  • @NayanaAdassuriya see §13.5.7. – juanchopanza Oct 05 '12 at 06:18
  • @chris : thank you. So you are call call a function without parameter. but it's signature have a parameter. How come this? – Nayana Adassuriya Oct 05 '12 at 06:21
  • @NayanaAdassuriya if you call the operator explicitly, you would have to pass a dummy `int` argument. I must say, I find this dummy parameter convention rather "hacky". – juanchopanza Oct 05 '12 at 07:12
  • Note: in N4687, it is in 16.5.7 – Chen Li Nov 25 '17 at 06:47