C++運算符重載

#include <iostream>
using namespace std;

class INT
{


 // public:
  friend ostream& operator<< (ostream& os, const INT& i);             //  必須聲明爲friend
  
  INT(int i):m_i(i){}
  
  INT& operator++()
  {
   ++(this->m_i);
   return *this;
  }
  
  const INT operator++(int)
  {
   INT temp =*this;
   ++(*this);
   return temp;
  }
  
  INT& operator--()
  {
   --(this->m_i);
   return *this;
  }
  
  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;
  
  return 0;
 }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章