1. 程式人生 > >operator++()和operator++(int)的區別

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++();