C++:为前后增量重载++

nul*_*ull 42 c++ operator-overloading

我们可以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; */
    } */
};
Run Code Online (Sandbox Code Playgroud)

我们不能仅仅基于返回类型重载函数,并且即使我们将其视为允许的,它也不能解决问题,因为调用解析的模糊性.

由于提供了运算符重载以使内置类型的行为类似于用户定义的类型,为什么我们不能同时为我们自己的类型利用前置和后置增量.

jua*_*nza 77

增量运算符的后缀版本采用伪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
}
Run Code Online (Sandbox Code Playgroud)

  • @ajay它是用语言定义的.当你调用`i ++`时,它将选择版本`operator ++(int)`.这有点像黑客,有点像它完成的方式. (35认同)
  • 但编译器如何执行此映射,因为我们在使用++时不传递任何参数(int). (6认同)
  • 当重载函数(即具有两个具有相同名称的不同函数)时,编译器不能仅基于返回类型将它们区分开来,分别告诉它们的是传递给它们的参数的数量和类型.这就是后缀增量具有"人工"添加的int参数的原因. (2认同)

Cas*_*Cow 20

类型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;
}
Run Code Online (Sandbox Code Playgroud)

(你也可以调用一个常用函数来执行增量,或者如果它是一个简单的单行代码,就像成员上的++一样,只需在两者中执行)


And*_*owl 12

为什么我们不能同时为我们自己的类型提供前后增量.

您可以:

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
  }
Run Code Online (Sandbox Code Playgroud)


陳 力*_*陳 力 7

[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++;
}
Run Code Online (Sandbox Code Playgroud)