作用: 通过重载递增运算符,实现自己的整型数据
#include<iostream>
using namespace std;
#include<string>
//递增运算符重载,实现自己的整型数据
//自定义的整型
class MyInteger
{
friend ostream &operator <<(ostream &out, MyInteger &p);
public:
MyInteger()
{
m_Num = 0;
}
//重载前置++运算符 返回引用是一直对一个数据进行递增操作
MyInteger &operator++()
{
//先进行++运算
m_Num++;
//再将自身做一个返回
return *this;
}
//重载后置++运算符
MyInteger operator++(int)//int为占位符 可以用于区分前置和后置递增
{
//先 记录当时结果
MyInteger temp = *this;
//后 递增
m_Num++;
//最后将记录结果做返回
return temp;
}
private:
int m_Num;
};
//重载左移运算符
ostream &operator <<(ostream &out, MyInteger &p)
{
cout << "p.m_Num=" << p.m_Num;
return out;
}
//测试案例1
void test1()
{
MyInteger myint;
cout << ++(++myint)<<endl;
cout << myint << endl;
cout <<(myint++)++ << endl;
cout << myint << endl;
}
int main()
{
test1();
return 0;
}