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;
}
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;
}