简体   繁体   中英

Purpose of Dummy Parameter in Postfix Operator Overload? c++

When overloading the postfix operator, I can do something simple like

Class Foo
{
private: 
   int someBS;
public:
   //declaration of  pre &postfix++
   Foo operator++();
   //rest of class not shown
};

Prefix doesn't need to take any parameters, so when I define it, something like

Foo Foo::operator()
{
   someBS ++;
   return *this;
}

and it makes perfect sense to me.

When I go to define the postfix overload I have to include a dummy int parameter

Foo Foo::operator++(int)
{
   Foo temp = *this;
   someBS ++;
   return temp;
}

My question is why? I don't ever use it in the method. The prefix operator doesn't require one. The postfix returning the temp value is not dependent on the dummy parameter. I know that if I want to overload a postfix operator that's how it's done, I just want to know the reason behind.

The dummy parameter is simply there to distinguish between the postfix and prefix operators. The name ++ or -- is the same in both cases, so there has to be some way to specify which one you're defining. Adding a dummy parameter is perhaps not elegant, but any alternatives would probably have required inventing new syntax (perhaps a postfix keyword, which would break code that uses postfix as an identifier).

Citing C++reference here:

Prefix versions of the built-in operators return references and postfix versions return values, and typical user-defined overloads follow the pattern so that the user-defined operators can be used in the same manner as the built-ins.

Explaining this logically:

int a = 3;
int b = 0;

int c = a + ++b;
int d = a++ + b;

In the third line, ++b gives you a reference to the updated value; in the fourth line, that's not possible: a has to be increased, so a value-copy is made, and added to b . That dictates different semantics for the operators.

In fact, just to avoid having another operator name:

The int parameter is a dummy parameter used to differentiate between prefix and postfix versions of the operators. When the user-defined postfix operator is called, the value passed in that parameter is always zero, although it may be changed by calling the operator using function call notation (eg, a.operator++(2) or operator++(a, 2)).

To quote cppreference :

The int parameter is a dummy parameter used to differentiate between prefix and postfix versions of the operators.

The quote says it all, because how else would you differentiate prefix and postfix?

Let's just say that you don't need the int parameter, then the prefix ( Foo operator++() ) and postfix ( Foo operator++() ) would be exactly the same! How would the compiler know which one you meant? That's why there is this dummy int parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM