#include<iostream>
using namespace std;
//递增运算符重载 ++
//自定义的整型
class MyInteger {
friend ostream &operator<<(ostream& cout, MyInteger myint);
public:
MyInteger() {
m_Num = 0;
}
//重载++前置运算符
MyInteger& operator++() {//返回引用是为了对一个数据进行操作 引用起别名后指向的是一个数据
m_Num++;//先进行++运算
return *this;//再将自身做返回
}
//重载后置++运算符
//void operator++(int) int代表占位参数,可以用于区分前置和后置递增
MyInteger operator++(int) {
//先返回结果
MyInteger temp = *this;
//后递增
m_Num++;
//最后将记录的结果返回
return temp;
}
private:
int m_Num;
};
//重载<<运算符
ostream& operator<<(ostream &cout, MyInteger myint) {
cout << myint.m_Num ;
return cout;
}
void test01() {
MyInteger myint;
cout << ++(++myint) << endl;
cout << myint << endl;
}
void test02() {
MyInteger myint;
cout << myint++ << endl;
cout << myint << endl;
}
int main()
{
//test01();
//int a = 0;
//cout << ++(++a) << endl;
//cout << a << endl;
test02;
system("pause");
return 0;
}
main函数中,调用test02函数,必须要带(),也就是test02(),带上()就能正常执行test02函数了。
为什么没有()编译程序没有报错,还能正常运行,就不清楚什么原因了。