做个实验就明白了:
#include <iostream>
#include <utility>
class Int {
public:explicit Int(int i_) : i(i_){}Int operator++(int inc) { // 后置std::cout << "后置" << std::endl;Int tmp = *this;i += 1;return tmp;}Int& operator++() { // 前置std::cout << "前置" << std::endl;i += 1;return *this;}public:int i;
};int main() {Int b(1);std::cout << (b++).i << std::endl;std::cout << "------------------" << std::endl;std::cout << (++b).i << std::endl;return 0;
}
运行结果:
后置
1
------------------
前置
3