繁体   English   中英

为前后增量重载 ++

[英]Overloading ++ for both pre and post increment

我们可以为前增量和后增量重载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参数的值为零

例:

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++;
}
#include<iostream>
using namespace std;

class increment{
int a;
public:
increment(int x)
{ a=x; }

void operator ++(){
cout<<"pre-increment:";
cout<<++a;}

void operator ++(int){                  /*post version of increment operator takes  int as a dummy parameter*/

 cout<<endl<<"post-increment:";
 cout<<a++;}
};


int main(){
increment o1(4);   
increment o2(4);
++o1;       //pre-increment
o2++;       //post-increment

}

输出:
预增:5
后增加:4

C++ 遵循一定的规则。

#include <iostream>

class sample {
public:
  int a; 
  sample(int val): a(val) {}
  void operator ++(int) { a += 3;} //post-increment
  void operator ++() { a += 4;} //pre-increment
  void print() {std::cout << a << std::endl; }
};

int main() {
   sample s1(5);
   sample s2(5); 
   s1++;
   ++s2;
   s1.print();
   s2.print();
}

此代码输出:8 9。对于后增量,我们需要在括号内添加“int”。 因此,该 C++ 确定了是前增量还是后增量的差异。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM