赋值运算符在返回语句中的含义是什么,例如return t = ...?

Nik*_*las 7 c++ enums return

我有疑问,在我的代码示例中返回赋值表达式意味着什么?我有一个枚举,我已经覆盖了++:运算符.因此,在我的简短示例中可以在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".

Jea*_*bre 6

在返回指令中,操作员分配t哪个是引用(修改它)然后返回值.

这就是增量运算符的作用:同时修改并返回引用,以便可以在另一个操作中使用递增的值.


zvo*_*one 5

t = Traficlight::yellowTraficlight::yellowt。该表达式的结果也是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实际上是相关的。