递增运算符重载
#include <iostream>
using namespace std;
//声明类
class wood
{
friend ostream& operator<<(ostream& cout, wood myint);
public:
//默认构造函数将m_Num初始化为0
wood()
{
m_Num = 0;
}
//前置递增++
//如果不用引用,就是调用重载函数,调用完之后就销毁(因为放在栈区)
//引用是别名,调用地址
wood& operator++()
{
//先++
this->m_Num++;
//后返回
return *this; //返回调用该函数的对象本身
}
//后置递增++
wood operator++(int)
{
//先 记录当前
wood temp = *this;
//后 ++
m_Num++;
//再 返回原先
return temp;
}
private:
int m_Num;
};
//重载<<运算符
ostream& operator<<(ostream& cout, wood myint)
{
cout << myint.m_Num;
return cout;
}
//测试函数
void test01()
{
//测试
wood myint;
cout << myint << endl;
cout << ++myint << endl;
cout << myint++ << endl;
cout << myint << endl;
}
int main()
{
test01();
return 0;
}