zoukankan      html  css  js  c++  java
  • increment/decrement/dereference操作符

    
    

    #include <iostream>

    using namespace std;

    class INT
    {
        friend ostream& operator<<(ostream& os, const INT& i);
    
    public:
        INT(int i) : m_i(i) {};
    
        //prefix: increment and then fetch
        INT& operator++()
        {
            ++(this->m_i);
            return *this;
        }
    
        //fetch and then increment
        const INT operator++(int)
        {
            INT tmp = *this;
            ++(*this);
            return tmp;
        }
    
        //prefix:decrement and then fetch
        INT& operator--()
        {
            --(this->m_i);
            return *this;
        }
    
        //prefix : fetch and then decrement
        const INT operator--(int)
        {
            INT temp = *this;
            --(*this);
            return temp;
        }
    
        //dereference
        int& operator*() const
        {
            return (int&)m_i;
        }
    
    private:
        int m_i;
    };
    
    ostream& operator<<(ostream& os, const INT& i)
    {
        os << '[' << i.m_i << ']';
        return os;
    }
    
    int main()
    {
        INT I(5);
        cout << I++ << endl;
        cout << ++I<<endl;
        cout << I--<<endl;
        cout << --I<<endl;
        cout << *I<<endl;
    
        getchar();
        return 0;
    }

    运行结果:

  • 相关阅读:
    1048 石子归并
    高精度算法小结
    3117 高精度练习之乘法
    UVa 11809
    3115 高精度练习之减法
    3116 高精度练习之加法
    “da shen” in my heart
    爱是怀疑!
    普通disco
    崇拜
  • 原文地址:https://www.cnblogs.com/hpcpp/p/6404152.html
Copyright © 2011-2022 走看看