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

C++: overloading ++ for both pre and post increment

是否可以超负荷operator++进行预增量和后增量?即正确调用SampleObject++++SampleObject结果。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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参数来消除歧义:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 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型的前增量和后增量的标准模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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;
}

(您也可以调用一个执行增量的公共函数,或者如果它是一个简单的一行程序,比如对一个成员使用++,只需同时调用这两个函数即可)


why we can't avail both pre and post increment for our own types at the same time.

你可以:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
  }

[N468 7]

16.5.7

The user-defined function called operator++ implements the prefix and postfix ++ operator. If this function is a non-static member function with no parameters, or a non-member function with one parameter, it defines the prefix increment operator++ for objects of that type. If the function is a non-static member function with one parameter (which shall be of type int) or a non-member function with two parameters (the second of which shall be of type int), it defines the postfix increment operator ++ for objects of that type. When the postfix increment is called as a result of using the ++ operator, the int argument will have value zero
[Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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++;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#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