C++中++,--操作符重载需要说明是++(--)在操作数前面,还是在操作数后面,区别如下:
代码经过测试无误(起码我这里没问题^_^)

 Code
Code1
 #include <iostream>
#include <iostream>2
 #include <cstdlib>
#include <cstdlib>3
 using namespace std;
using namespace std;4
 template<typename T> class A
template<typename T> class A5


 {
{6
 public:
public:7

 A(): m_(0)
    A(): m_(0) {
{8
 }
    }9
 // +
    // +10
 const T operator + (const T& rhs)
    const T operator + (const T& rhs)11

 
     {
{12
 // need to be repaired , but see it is only a demo
           // need to be repaired , but see it is only a demo13
 return (this->m_ + rhs);
           return (this->m_ + rhs);14
 }
    }15
 // -
    // -16

 const T operator - (const T& rhs)
    const T operator - (const T& rhs) {
{17
 // need to be repaired , but see it is only a demo
           // need to be repaired , but see it is only a demo18
 return (this->m_ - rhs);
             return (this->m_ - rhs);19
 }
    }20

 T getM()
    T getM() {
{21
 return m_;
      return m_;22
 }
    }23
 
    24
 // ++在前的模式,这里返回的是引用 ,准许++++A
    // ++在前的模式,这里返回的是引用 ,准许++++A25

 A& operator ++ ()
    A& operator ++ () {
{26
 (this->m_)++;
           (this->m_)++;27
 return *this;
           return *this;28
 }
    }29
 // ++ 在后,这里返回的是一个新的A类型变量,且不可改变
    // ++ 在后,这里返回的是一个新的A类型变量,且不可改变30
 // 目的是防止出现 A++++情况
    // 目的是防止出现 A++++情况31

 const A operator ++(int a)
    const A operator ++(int a) {
{32
 A<T> b = *this;
           A<T> b = *this;33
 (this->m_)++;
           (this->m_)++;34
 return b;
           return b;35
 }
    }36
 private:
private:37
 T m_;
    T m_;38
 };
};39

40

41

 int main(void)
int main(void) {
{42
 int i = 0;
    int i = 0;43
 cout<<++++i<<endl;
    cout<<++++i<<endl;44
 // i++++ is not allowed
    // i++++ is not allowed45
 A<int> a;
    A<int> a;46
 A<int> b = ++a;
    A<int> b = ++a;47
 cout<<b.getM()<<endl;
    cout<<b.getM()<<endl;48
 A<int> c = a++;
    A<int> c = a++;49
 cout<<c.getM()<<endl;
    cout<<c.getM()<<endl;50
 cout<<a.getM()<<endl;
    cout<<a.getM()<<endl;51
 int t = a+2;
    int t = a+2;52
 cout<<t<<endl;
    cout<<t<<endl;53
 system("pause");
    system("pause");54
 return 0;
    return 0;    55
 
    56
 }
}