重载++前和后增量

Overloading ++ for both pre and post increment

本文关键字:重载      更新时间:2023-10-16

我们可以重载operator++进行前递增和后递增吗?即调用SampleObject++++SampleObject会得到正确的结果。

class CSample {
 public:
   int m_iValue;     // just to directly fetch inside main()
   CSample() : m_iValue(0) {}
   CSample(int val) : m_iValue(val) {}
   // Overloading ++ for Pre-Increment
   int /*CSample& */ operator++() { // can also adopt to return CSample&
      ++(*this).m_iValue;
      return m_iValue; /*(*this); */
   }
  // Overloading ++ for Post-Increment
 /* int operator++() {
        CSample temp = *this;
        ++(*this).m_iValue;
        return temp.m_iValue; /* temp; */
    } */
};

不能只根据返回类型重载函数,而且即使允许这样做,也不能解决问题,因为重载解析存在歧义。

既然提供了操作符重载使内置类型表现得像用户定义的类型,为什么不能同时对自己的类型使用前置和后增量呢?

为了消除歧义,自增运算符的后缀版本接受一个假int参数:

// prefix
CSample& operator++()
{
  // implement increment logic on this instance, return reference to it.
  return *this;
}
// postfix
CSample operator++(int)
{
  CSample tmp(*this);
  operator++(); // prefix-increment this instance
  return tmp;   // return value before increment
}

类型T

的递增前和递增后的标准模式
T& T::operator++() // pre-increment, return *this by reference
{
 // perform operation

 return *this;
}
T T::operator++(int) // post-increment, return unmodified copy by value
{
     T copy(*this);
     ++(*this); // or operator++();
     return copy;
}

(您也可以调用一个通用函数来执行增量,或者如果它是一个简单的单行代码,如对成员的++,则只需同时执行)

为什么我们不能同时对自己的类型使用前后自增操作

您可以:

class CSample {
public:
     int m_iValue;
     CSample() : m_iValue(0) {}
     CSample(int val) : m_iValue(val) {}
     // Overloading ++ for Pre-Increment
     int /*CSample& */ operator++() {
        ++m_iValue;
        return m_iValue;
     }
    // Overloading ++ for Post-Increment
    int operator++(int) {
          int value = m_iValue;
          ++m_iValue;
          return value;
      }
  };
  #include <iostream>
  int main()
  {
      CSample s;
      int i = ++s;
      std::cout << i << std::endl; // Prints 1
      int j = s++;
      std::cout << j << std::endl; // Prints 1
  }

[N4687]

16.5.7

用户定义函数operator++实现了前缀++和后缀++操作符。如果此函数是没有形参的非静态成员函数,或者只有一个形参的非成员函数,则为该类型的对象定义前缀自增操作符++。如果函数是带一个形参(int类型)的非静态成员函数或带两个形参(第二个形参为int类型)的非成员函数,则为该类型的对象定义后缀自增操作符++。当使用++操作符调用后缀自增时,int实参的值为0

例子:

struct X {
  X&   operator++();    // prefix ++a
  X    operator++(int); // postfix a++
};
struct Y { };
Y&   operator++(Y&);      // prefix ++b
Y    operator++(Y&, int); // postfix b++
void f(X a, Y b) {
  ++a; // a.operator++();
  a++; // a.operator++(0);
  ++b; // operator++(b);
  b++; // operator++(b, 0);
  a.operator++();     // explicit call: like ++a;
  a.operator++(0);    // explicit call: like a++;
  operator++(b);      // explicit call: like   ++b;
  operator++(b, 0);   // explicit call: like b++;
}