說我有一類這樣重載一元運算符的後綴
class foo
{
public:
char* operator+() //unary operator - prefix before instance
{
return "SomeChar";
}
};
現在我可以用它作爲這樣
foo d;
std::cout << +d; //unary operator used as prefix allowed
現在,如果我想用一元運算符爲後綴,然後根據到this頁面
正如你在這種情況下,我們看到使用int作爲 修復後,當僞參數我們重新定義了一元增量(++) 的函數和遞減( - )重載運算符。你必須記住int 不是一個整數,而只是一個虛擬參數。您可以將其作爲 信號發送給編譯器,以創建 運算符的修復後符號。
我使用+而不是++,因爲兩者都是一元運算符
我可以做到以下幾點
class foo
{
public:
char* operator+() //unary operator - prefix before instance
{
return "SomeChar";
}
//Added this for postfix unary operation
char* operator+ (int) //unary operator - postfix before instance
{
return "SomeChar";
}
};
但是這個(後綴)不起作用
foo d;
std::cout << (d+) ; //unary operator used as post fix ERROR (Expected an expression)
對此問題有何建議/評論?
沒有後綴+。您不能重載不存在的操作員。 – 2014-09-24 08:22:39