c++前置形式和后置形式的自增
“++x”自增,返回自增后的值;
“x++”用tmp保存当前值,自增,返回tmp。
class Interger {
public:
int value = 0;
int operator++() { // 前置
this->value++;
return this->value;
}
int operator++(int) { // 后置
int tmp = this->value;
this->value++;
return tmp;
}
};
#include <iostream>
using namespace std;
int main() {
Interger myInt;
cout << myInt.value << endl;
cout << myInt++ << endl;
cout << myInt.value << endl;
cout << ++myInt << endl;
}

浙公网安备 33010602011771号