unaryOperators& operator++ (unaryOperators &one)
^^
是非構件前綴元增量運算符。
非成員後綴一元增量算子需要額外的int
作爲策略執行參數。
unaryOperators operator++ (unaryOperators &one, int)
參考:
C++ 03標準13.5.7遞增和遞減[over.inc]
The user-defined function called operator++ implements the prefix and postfix ++ operator. If this function is a member function with no parameters, or a non-member function with one parameter of class or enumeration type, it defines the prefix increment operator ++ for objects of that type. If the function is a member function with one parameter (which shall be of type int
) or a non-member function with two parameters (the second of which shall be of type int
), it defines the postfix increment operator ++ for objects of that type. When the postfix increment is called as a result of using the ++ operator, the int argument will have value zero.125)
[Example:
class X {
public:
X& operator++(); // prefix ++a
X operator++(int); // postfix a++
};
class Y { };
Y& operator++(Y&); // prefix ++b
Y operator++(Y&, int); // postfix b++
void f(X a, Y b) {
++a; // a.operator++();
a++; // a.operator++(0);
++b; // operator++(b);
b++; // operator++(b, 0);
a.operator++(); // explicit call: like ++a;
a.operator++(0); // explicit call: like a++;
operator++(b); //explicit call: like ++b;
operator++(b, 0); // explicit call: like b++;
}
—end example]
爲什麼你不試試? – 2012-01-06 07:06:04
@KirilKirov我想知道「原因」和「邏輯」。嘗試只會告訴我輸出。 – 2012-01-06 07:07:05
我明白了。順便說一句,我沒有投票。嗯,其實我很驚訝。我刪除了類中的聲明,在'unaryOperators operator ++(unaryOperators&one)',添加了log,創建了對象'unaryOperators a'並嘗試了'a ++; ++ a;'**並且都打印了添加的日誌!**。有趣的 – 2012-01-06 07:11:50