您的位置:首页 > 编程语言 > C语言/C++

C++运算符重载

2012-07-03 12:10 267 查看
#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;

 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ os include