首页 > 其他 > 详细

递增运算符重载

时间:2021-01-14 15:06:23      阅读:27      评论:0      收藏:0      [点我收藏+]
#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;
}
    

 

递增运算符重载

原文:https://www.cnblogs.com/loliconsk/p/14276516.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!