为前缀和后缀增量重载 ++ 运算符

58
我们能否重载前缀和后缀的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; */
    } */
};

我们不能仅基于返回类型来重载函数,即使我们将其视为允许的,也无法解决重载分辨率中的歧义问题。
由于运算符重载是提供给内置类型以表现为用户定义类型的,为什么我们不能同时使用自增和后自增来操作我们自己的类型呢?

就我个人而言,我从不使用后增量(post increment)写代码。我认为它的使用可能会导致非常复杂的代码。 - abergmeier
2
@LCIDFire 如果没有它,实现迭代器会相当棘手。 - juanchopanza
在C++20中,您可以使用默认比较(又称“飞船运算符”)。 - DevSolar
4个回答

107
后缀版本的递增运算符需要一个虚拟的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
}

9
编译器如何进行这种映射,既然我们在使用++时没有传递任何参数(int)? - null
48
在语言中已经定义了这个操作。当你调用i++时,它会选择版本为operator++(int)的操作。这有点像一个技巧,但事实就是这样做的。 - juanchopanza
3
当函数重载(即使用相同名称但不同的函数)时,编译器不能仅根据返回类型来区分它们,唯一能区分它们的是传递给它们的参数数量和类型。这就是为什么后缀自增运算符会添加一个 int 类型的参数来进行“人为”的区分。 - pav
1
在普通函数中,CSample的析构函数会在函数返回后被调用。但是在后置运算符重载中不会发生这种情况吗? - paarandika
1
@paarandika 从函数返回的值可能会被省略复制,这种情况下就不会有复制构造函数或析构函数调用。但是,如果复制没有被省略,那么在形式上将会有一个析构函数调用(除了平凡可销毁类型和“_as-if_”规则)。 - juanchopanza
显示剩余2条评论

29

类型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;
}

(您也可以调用一个通用函数来执行增量操作,或者如果是像成员上的++这样的简单一行代码,则在两个位置都执行它)

13
为什么我们不能同时使用前置和后置自增运算符来处理自定义类型。
你可以:
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
  }

9

[N4687]

16.5.7

用户定义的函数operator++实现了前缀和后缀++运算符。如果此函数是一个无参数的非静态成员函数,或者是一个带有一个参数的非成员函数,则它定义了该类型对象的前缀递增运算符++。如果函数是具有一个参数(应为int类型)的非静态成员函数或具有两个参数(第二个应为int类型)的非成员函数,则其定义了该类型对象的后缀递增运算符++。当由于使用++运算符而调用后缀递增时,int参数将具有值零。

示例:

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++;
}

(b++2)会发生什么? - AnnoyinC
@AnnoyinC,即使bint类型,这段代码仍无法编译通过。https://godbolt.org/z/dEPzM8Whh - Marek R

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接