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;
}

  

posted @ 2021-06-10 21:09  _骤雨  阅读(85)  评论(0)    收藏  举报