zoukankan      html  css  js  c++  java
  • 自增自减操作符

    #include<iostream>
    using namespace std;

    class INT{
     friend ostream& operator<<(ostream& os,const INT& i);
    public:
     INT(int i):m_i(i){};
     //prefix ++
     INT& operator++()
     {
         ++(this->m_i);
      return *this;
     }
     //postfix ++
     const INT operator++(int)
     {
          INT temp=*this;
       ++(*this);
       return temp;
     }
     //prefix --
     INT& operator--()
     {
         --(this->m_i);
      return *this;
     }
     //postfix --
     const INT operator--(int)
     {
         INT temp=*this;
      --(*this);
      return temp;
     }
     int& operator*() const
     {
         return (int&)m_i;
     }
       
    private:
     int m_i;
    };

    ostream& operator<<(ostream& os,const INT& i)
    {
        os<<'['<<i.m_i<<']'<<endl;
     return os;
    }
    int main()
    {
     INT I(5);
      
        cout<<I++;
     cout<<++I;
     cout<<I--;
     cout<<--I;
     cout<<*I;
     cout<<endl;
        system("pause");
     return 0;
    }

  • 相关阅读:
    第八周作业
    第八周上机练习
    第七周作业
    第七次上机练习
    第六周作业
    4.9上机作业
    第五周作业
    第四次JAVA作业
    第四周作业
    第十六次作业
  • 原文地址:https://www.cnblogs.com/yanglf/p/3037169.html
Copyright © 2011-2022 走看看