我有疑问,在我的代码示例中返回赋值表达式意味着什么?我有一个枚举,我已经覆盖了++:运算符.因此,在我的简短示例中可以在ligth之间切换 - 但是代码中有一部分我不明白.代码编译并正常工作.
码:
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;
}
Run Code Online (Sandbox Code Playgroud)
"返回t = Traficlight :: yellow"是什么意思,为什么我只能返回"Traficlight :: yellow".
t = Traficlight::yellow写Traficlight::yellow成t。该表达式的结果也是Traficlight::yellow,所以:
return t = Traficlight::yellow;
Run Code Online (Sandbox Code Playgroud)
等于:
t = Traficlight::yellow;
return t;
Run Code Online (Sandbox Code Playgroud)
在上面的函数中,引用t作为参数接收,所以改变值t实际上是相关的。