C++递增运算符重载

#include<iostream>
using namespace std;
class MyInteger {
public:
    MyInteger() {
        m_Num = 0;
    }
    //重载前置递增运算符
    MyInteger& operator++() {
        m_Num++;
        return *this;
    }
    //重载后置递增运算符
    MyInteger operator++(int) {
        MyInteger tmp = *this;
        m_Num++;
        return tmp;
    }
    friend ostream& operator<<(ostream& cout, const MyInteger& myint);
private:
    int m_Num;
};
ostream& operator<<(ostream& cout, const MyInteger& myint) {
    cout << myint.m_Num;
    return cout;
}
int main() {
    MyInteger myint;
    cout << ++++++myint << endl;//输出3
    cout << myint << endl;//输出3 
    cout << myint++++++ << endl;//输出3 
    cout << myint << endl;//输出4 
    system("pause");
    return 0;
}

 

posted on 2020-03-13 21:27  ~明月几时有  阅读(326)  评论(0)    收藏  举报