作用:通过重载递增运算符,实现自己的整型数据
总结:前置递增返回引用,后置递增返回值
//左移运算符重载class MyInt{friend ostream& operator<<(ostream& out, MyInt myint);public:MyInt(int a = 0) :m_Num(a) {}//重载前置++运算符 返回引用是为了一直对一个数据进行操作//MyInt& operator++()//{// //先进行++运算// m_Num++;// //再将自身返回// return *this;//}//重载后置++运算符//operator++(int) int代表占位参数,可以用于区分前置和后置递增,并且只能写int,不能写别的数据类型MyInt operator++(int){//先 记录当时结果MyInt tmp = *this;//后 递增m_Num++;//最后将记录结果做返回return tmp;}int m_Num;};//重载<<运算符ostream& operator<<(ostream& out, MyInt myint){out << myint.m_Num;return out;}//++运算符重载全局函数MyInt& operator++(MyInt &myint){//先进行++运算myint.m_Num++;//再将自身返回return myint;}void test01(){MyInt i(10);cout << ++(++i) << endl;}void test02(){MyInt myint;cout << myint++ << endl;cout << myint << endl;}int main(){test01();test02();system("pause");return 0;}
