重载后缀和前缀运算符

发布于 2024-12-10 00:35:04 字数 1314 浏览 1 评论 0原文

请考虑以下代码:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

千秋岁 2024-12-17 00:35:04

预自增和后自增是两个不同的运算符,需要单独的重载。

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.

妖妓 2024-12-17 00:35:04

在预自增/自减和后自增/自减中,区别在于重载函数

operator++()         => Prefix Increment
operator--()         => Prefix Decrement

operator++(int)      => Postfix Increment
operator--(int)      => Postfix Decrement

返回类型中仅虚拟参数可能相同。您还可以参考: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

operator++()         => Prefix Increment
operator--()         => Prefix Decrement

operator++(int)      => Postfix Increment
operator--(int)      => Postfix Decrement

return type may be same. you can also refer: http://www.tutorialspoint.com/cplusplus/increment_decrement_operators_overloading.htm

极度宠爱 2024-12-17 00:35:04

与任何函数一样,运算符由签名来标识。函数/运算符名称之前的返回类型和修饰符不包含在其中。您的运算符名称在这里

operator++()
operator++(int)

这是编译器区分两者的唯一方法。至于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

operator++()
operator++(int)

This is the only way the compiler distinguishes between the two. As for the Digit& and Digit return values; They are needed because of the way ++x, and x++ are supposed to operate.

梦忆晨望 2024-12-17 00:35:04

在 C++ 中,函数/方法不能通过返回类型重载,只能通过参数列表重载。忽略前缀和后缀运算符是运算符的事实,想象一下,如果它们只是简单的其他函数,编译器将如何根据返回类型确定要使用哪个函数?例如,

int x = 2;

const int DoIt()
{
    return 1;
}

int& DoIt()
{
    return x;
}

int y = DoIt();

由于运算符重载本质上只是函数,因此编译器无法通过返回类型来区分它们。

请参阅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.

int x = 2;

const int DoIt()
{
    return 1;
}

int& DoIt()
{
    return x;
}

int y = DoIt();

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文