标签:c++
#include <iostream>
using namespace std;
class INT
{
public:
friend ostream& operator<<(ostream& os, const INT& i);
INT(int i) : m_i(i) {};
INT& operator++()
{
++(this->m_i);
return *this;
}
const INT operator++(int)
{
INT temp = *this;
++(*this);
return temp;
}
const 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<< ‘]‘;
return os;
}
int main()
{
INT I(5);
cout<<I++<<endl; //operator++(int)
cout<<++I<<endl;
cout<<I--<<endl;
cout<<--I<<endl;
cout<<*I<<endl;
return 0;
}
运行结果:
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:c++
原文地址:http://blog.csdn.net/nizhannizhan/article/details/47043867