#include<iostream>
using namespace std;
//重载递增运算符
//自定义整形
class MyIntrger {
friend ostream& operator<<(ostream& ocut, MyIntrger cout);
public:
MyIntrger() {
m_num = 0;
}
//重载前置++运算符
MyIntrger& operator++() {//返回引用是为了一直对一个数据进行递增
m++;//先进行++运算
return *this;//再将自身做返回
}
//重载后置++运算符
MyIntrger operator++(int) {//int 代表占位参数,可以用于区分前置和后置递增
//先 返回结果 记录当时结果
MyIntrger temp = *this;
//后 递增
m_num++;
//最后将记录的结果做返回
return temp;
}
private:
int m_num;
};
//重载<<运算符
ostream& operator<<(ostream& ocut, MyIntrger p) {
cout << p.m_num;
return cout;
}
void test01() {
MyIntrger p;
cout <<++ p << endl;
cout << p << endl;
}
void test02() {
MyIntrger p;
cout << p++ << endl;
cout << p << endl;
}
int main() {
test01();
test02();
system("pause");
return 0;
}
标签:MyIntrger,cout,++,递增,运算符,int,重载 From: https://blog.51cto.com/u_15729005/5732484