运算符++()nothrow不编译

operator++() nothrow does not compile

本文关键字:编译 nothrow 运算符      更新时间:2023-10-16

为什么我不能使运算符++()另一行?

这可能是使用后缀++运算符(相对于前缀++运算符)的少数优点之一。

例如,此代码不编译

class Number
{
public:
    Number& operator++ ()     // ++ prefix
    {
        ++m_c;
        return *this;
    }
    Number operator++ (int) nothrow  // postfix ++
    {
        Number result(*this);   // make a copy for result
        ++(*this);              // Now use the prefix version to do the work
        return result;          // return the copy (the old) value.
    }
    int m_c;
};

顺便说一句,后缀操作符也可以是线程安全的。

nothrow是一个常量,用于传递给运算符new,以指示new不应在出错时引发异常。

我认为你想要的是无可挑剔的。