2012-01-06 81 views
2

類聲明:超載運營++

class unaryOperators 
{ 
    public: 
     int i; 

     unaryOperators (int tempI = 0) 
     { 
      i = tempI; 
     } 

     unaryOperators operator++ (int); 
     unaryOperators operator++(); 
}; 

這是否全局定義對應於重載的運算符的後綴或前綴++版本?爲什麼?

unaryOperators operator++ (unaryOperators &one) 
{ 
    return one; 
} 
+0

爲什麼你不試試? – 2012-01-06 07:06:04

+0

@KirilKirov我想知道「原因」和「邏輯」。嘗試只會告訴我輸出。 – 2012-01-06 07:07:05

+0

我明白了。順便說一句,我沒有投票。嗯,其實我很驚訝。我刪除了類中的聲明,在'unaryOperators operator ++(unaryOperators&one)',添加了log,創建了對象'unaryOperators a'並嘗試了'a ++; ++ a;'**並且都打印了添加的日誌!**。有趣的 – 2012-01-06 07:11:50

回答

6
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] 
+0

是的,沒錯。 – 2012-01-06 08:43:35

+0

@AnishaKaul:更新了完整性標準的參考。 – 2012-01-06 09:02:21

+2

@Als:優秀的回答:) – 2012-01-21 08:45:34

0

自由函數是前綴,因爲它缺少一個int參數。

Helpful guide for operator signatures.

+0

是的,但C++ faq說前綴版本不帶任何參數,但在這種情況下提供了一個參數。 – 2012-01-06 07:08:44

+0

並且在類中寫入上述函數會給出錯誤:'error:postfix'unaryOperators unaryOperators :: operator ++(const unaryOperators&)'必須以'int'作爲它的參數,這意味着編譯器不會將其作爲前綴。 – 2012-01-06 07:09:54

+0

預定義運算符在類中定義時不包含任何參數*。當在類的外部定義時,它需要一個參數 - 對要增加的東西的引用。 – cHao 2012-01-06 07:10:55

0

前增量的典型版本是:

T &operator++(T &) 

也就是說,通過參考返回操作數。後增加了一個未使用的int,因此您定義的全局operator++是預增量運算符。

+0

MSN在@Pubby的答案上看到我的評論。 – 2012-01-06 07:10:50

+0

不,由於該參數是隱含的,所以它不會接受T&參數。但是在這裏你定義了一個自由函數,所以沒有隱含的「this」。 – CashCow 2012-01-06 07:29:04

2

我認爲this會幫助你。

+0

在這裏包含答案的摘要會非常有幫助,而不僅僅是發佈一些鏈接到網絡上的某些材料,這些材料可能會/可能不會提供給未來的讀者。 – razlebe 2012-01-06 07:49:05

2

各運營商(即可以被重載作爲一個自由函數)在作爲一個自由函數重載時需要多一個參數。當作爲成員函數重載時,第一個參數對應於*this

bool AsMember::operator!() const; 
bool operator!(const AsFreeFunction&); 

bool AsMember::operator==(const AsMember& rhv) const; 
bool operator==(const AsFreeFunction& lhv, const AsFreeFunction& rhv); 

etc. 

增量運算符也不例外。

+0

是的,我想到了。 – 2012-01-06 08:13:41

1

全球重載運算符++函數需要的所有參數的明確規範,因此,如果重載運算符++是後綴,我們應該在另外添加一個默認int參數to distinguish postfix version from prefix的前提一個(它決定了功能需要應用的類型)

unaryOperators operator++ (unaryOperators &one, int dummy) 
{ 
    return one; 
} 

前綴全球重載操作++函數,我們需要指定唯一的參數的情況下,是前提一個(其確定在其上需要應用的功能的類型)

unaryOperators operator++ (unaryOperators &one) 
{ 
    return one; 
}