递增运算符重载

#include<iostream>
using namespace std;

//运算符递增重载

class myint
{
    friend ostream& operator<<(ostream& cout, myint a);
public:
    myint()
    {
        m_a = 10;
    }
    //前置递增运算符
    //返回引用是为了一直对一个数据进行递增
    myint& operator++()
    {
        //先做++运算
        m_a++;
        //再将自身返回
        return *this;
    }

    //int代表占位参数,可以用于区分前置和后置递增
    myint operator++(int)
    {
        //先记录当前结果
        myint temp = *this;
        //后递增
        m_a++;
        //最后将记录返回
        return temp;
    }
private:
    int m_a;
};

ostream& operator<<(ostream& cout, myint a)
{
    cout << "m_a = " <<a.m_a ;
    return cout;
}

int main(void)
{
    myint t;

    cout <<++( ++t) << endl;
    cout << t << endl;
}
    

 

posted @ 2021-01-14 12:13  loliconsk  阅读(106)  评论(0)    收藏  举报