#include <iostream>
using namespace std;
class sample
{
public:
sample(int i=0,int j=0)
{
x=i;y=j;
}
void print()
{
cout<<"x="<<x<<";y="<<y<<endl;
}
sample & operator++()
{
++x;++y;
return *this;
}
sample operator++(int)
{
sample oldValue=*this;
++x;++y;
return oldValue;
}
protected:
private:
int x,y;
};
void main()
{
sample s1(2,7);
// ++(++s1);s1.print();
// (s1++)++;s1.print();
((s1++)++).print();
(++(++s1)).print();
// int i=12;
// cout<<(i++)++<<endl;
}
首先注意前置和后置运算符的函数形式,后置的有伪参数int,前置的没有。
其次,(s1++)++在第一次后置自增运算符后返回一个sample对象(不是原来的s1),第二个后置自增运算符作用的是返回这个sample对象(不是原来的s1)。
输出结果:
x=2,y=7
x=5,y=10