operator++()和operator++(int)的区别

转自: https://blog.csdn.net/piaopiaohu123/article/details/7333771

class UPInt {

public:
 UPInt& operator++(); // ++ 前缀
 const UPInt operator++(int); // ++ 后缀
 UPInt& operator- -(); // – 前缀
 const UPInt operator- -(int); // – 后缀
 UPInt& operator+=(int); // += 操作
 …
};

UPInt i;

++i; // 调用 i.operator++();
i++; // 调用 i.operator++(0);
–i; // 调用 i.operator–();
i–; // 调用 i.operator–(0);

方便记忆:i++:++后面还要接东西就operator++(int)

++i:加号后面有i啦不用加东西了operator++();

猜你喜欢

转载自blog.csdn.net/winter_wu_1998/article/details/83868545