我有問題,這是什麼意思返回像我的代碼示例中的賦值表達式?我有一個枚舉,並且我覆蓋了++:操作符。所以我可以在簡短的例子中在ligths之間切換 - 但是我不明白代碼中有一部分。代碼編譯和工作正常。賦值運算符在返回語句中意味着什麼,如return t = ...?
代碼:
enum Traficlight
{green, yellow, red };
Traficlight& operator++(Traficlight& t)
{
switch (t)
{
case green: return t = Traficlight::yellow; //Here <--
case yellow: return t = Traficlight::red; //Here <--
case red: return t = Traficlight::green; //Here <--
default:
break;
}
}
int main()
{
Traficlight Trafic = Traficlight::green;
Trafic++;
if (Trafic == Traficlight::yellow)
{
cout << "Light is Yellow" << endl;
}
string in;
cin >> in;
}
什麼是 「返回T = Traficlight ::黃」 的意思,爲什麼不能我只是返回 「Traficlight ::黃」。
有關賦值運算符的返回值的更多信息,請參見:http://stackoverflow.com/questions/14697643/low-level-details-of-cc-assignment-operator-implementation-what-does- it-retu –
與t = x相同。返回x;'。 – 0x499602D2
這段代碼沒有編譯:你可以定義preincrement'operator ++',但是使用postincrement'operator ++'。 – aschepler