重载后缀和前缀运算符
请考虑以下代码:
#include <iostream>
using namespace std;
class Digit
{
private:
int m_digit;
public:
Digit(int ndigit = 0) {
m_digit = ndigit;
}
Digit& operator++(); // prefix
Digit& operator--(); // prefix
Digit operator++(int);
Digit operator--(int);
int get() const {
return m_digit;
}
};
Digit& Digit::operator++() {
++m_digit;
return *this;
}
Digit& Digit::operator--() {
--m_digit;
return *this;
}
Digit Digit::operator++(int) {
Digit cresult(m_digit);
++(*this);
return cresult;
}
Digit Digit::operator--(int) {
Digit cresult(m_digit);
--(*this);
return cresult;
}
int main() {
Digit cDigit(5);
++cDigit;
cDigit++;
cout << cDigit.get() << endl;
cout << cDigit.get() << endl;
return 0;
}
这里是实现的后缀和前缀运算符的两个版本,我读到差异是通过引入另一个所谓的虚拟参数来实现的,但我有一个问题。
如果我们查看这些运算符的声明,就会
Digit& operator++(); // prefix
Digit& operator--(); // prefix
Digit operator++(int);
Digit operator--(int);
发现它们的不同之处在于 &
字符,那么为什么需要虚拟参数呢?另外,在这两种情况下,运算符++
位于参数之前,这是否表明含义相同?
Please consider the following code:
#include <iostream>
using namespace std;
class Digit
{
private:
int m_digit;
public:
Digit(int ndigit = 0) {
m_digit = ndigit;
}
Digit& operator++(); // prefix
Digit& operator--(); // prefix
Digit operator++(int);
Digit operator--(int);
int get() const {
return m_digit;
}
};
Digit& Digit::operator++() {
++m_digit;
return *this;
}
Digit& Digit::operator--() {
--m_digit;
return *this;
}
Digit Digit::operator++(int) {
Digit cresult(m_digit);
++(*this);
return cresult;
}
Digit Digit::operator--(int) {
Digit cresult(m_digit);
--(*this);
return cresult;
}
int main() {
Digit cDigit(5);
++cDigit;
cDigit++;
cout << cDigit.get() << endl;
cout << cDigit.get() << endl;
return 0;
}
Here are two versions of postfix and prefix operators implemented, I read that the difference is made by introducing another so-called dummy argument, but I have a question.
If we look at the declaration of these operators,
Digit& operator++(); // prefix
Digit& operator--(); // prefix
Digit operator++(int);
Digit operator--(int);
they differ by the &
character, so why is a dummy argument necessary? Also, in both cases the operator ++
precedes the argument, and doesn't that suggest that the same thing is meant?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
预自增和后自增是两个不同的运算符,需要单独的重载。
C++ 不允许仅在返回类型上重载,因此像示例中那样具有不同的返回类型不足以消除这两种方法的歧义。
虚拟参数是 C++ 设计者选择的用于消除歧义的机制。
The pre- and post-increment are two distinct operators, and require separate overloads.
C++ doesn't allow overloading solely on return type, so having different return types as in your example wouldn't be sufficient to disambiguate the two methods.
The dummy argument is the mechanism that the designer of C++ chose for the disambiguation.
在预自增/自减和后自增/自减中,区别在于重载函数
返回类型中仅虚拟参数可能相同。您还可以参考:http://www.tutorialspoint.com/cplusplus/increment_decrement_operators_overloading.htm
In Pre-Increment/Decrement and Post-Increment/decrement, the difference is based on only dummy parameter in overloaded function
return type may be same. you can also refer: http://www.tutorialspoint.com/cplusplus/increment_decrement_operators_overloading.htm
与任何函数一样,运算符由签名来标识。函数/运算符名称之前的返回类型和修饰符不包含在其中。您的运算符名称在这里
这是编译器区分两者的唯一方法。至于
Digit&
和Digit
返回值;由于 ++x 和 x++ 的运行方式,需要它们。The operator, just as any function, is identified by the signature. The return type and modifiers before the function/operator name is not included in this. Your operators names are here
This is the only way the compiler distinguishes between the two. As for the
Digit&
andDigit
return values; They are needed because of the way ++x, and x++ are supposed to operate.在 C++ 中,函数/方法不能通过返回类型重载,只能通过参数列表重载。忽略前缀和后缀运算符是运算符的事实,想象一下,如果它们只是简单的其他函数,编译器将如何根据返回类型确定要使用哪个函数?例如,
由于运算符重载本质上只是函数,因此编译器无法通过返回类型来区分它们。
请参阅http://www.parashift.com/c++- faq-lite/operator-overloading.html#faq-13.14
In C++ functions/methods can't be overloaded by return type, only by parameter list. Ignoring the fact that the prefix and postfix operators are operators, imagine if they were just simple other functions, how would the compiler work out which to use based on the return type? E.g.
Since operator overloads are really just functions at heart, there's no way for the compiler to differentiate between them by return type.
See http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.14