简体   繁体   English

Postfix运算符重载中虚拟参数的目的? C++

[英]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当我去定义后缀重载时,我必须包含一个虚拟的 int 参数

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.返回temp值的后缀不依赖于虚拟参数。 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).添加一个虚拟参数可能并不优雅,但任何替代方案都可能需要发明新的语法(可能是一个postfix关键字,它会破坏使用postfix作为标识符的代码)。

Citing C++reference here:在此处引用C++ 参考

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;在第三行中, ++b为您提供了对更新值的引用; in the fourth line, that's not possible: a has to be increased, so a value-copy is made, and added to b .在第四行中,这是不可能的: a必须增加,因此创建了一个值副本,并将其添加到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. int 参数是一个虚拟参数,用于区分运算符的前缀和后缀版本。 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)).当调用用户定义的后缀运算符时,该参数中传递的值始终为零,尽管可以通过使用函数调用符号(例如,a.operator++(2) 或 operator++(a, 2))调用运算符来更改它.

To quote cppreference :引用cppreference

The int parameter is a dummy parameter used to differentiate between prefix and postfix versions of the operators. int 参数是一个虚拟参数,用于区分运算符的前缀和后缀版本。

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!假设您不需要int参数,那么前缀( Foo operator++() )和后缀( Foo operator++() )将完全相同! How would the compiler know which one you meant?编译器怎么知道你指的是哪一个? That's why there is this dummy int parameter.这就是为什么有这个虚拟int参数的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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