#include<stdio.h>
#include<iostream>
using namespace std;
class Object
{
private:
int value;
public:
Object(int a):value(a){}
Object& operator=(const Object& it)
{
if (this != &it)
{
this->value = it.value;
}
return *this;
}
Object operator++(int )
{
Object old = *this;
this->value++;
return old;
}
Object& operator++()
{
this->value++;
return *this;
}
int getValue()
{
return this->value;
}
};
int main()
{
Object object1(2);
Object object2(2);
//object1++;
//++object2;
cout << object1++.getValue() << endl;
cout << (++object2).getValue() << endl;
}
注意前置++与后置++之间的区别。
要注意以引用返回的情况。
什么时候用引用返回?
如果返回的是自身,用引用返回。
什么时候用值返回。
如果返回的是临时量或将亡值,用值返回。