Why does overloaded assignment operator return reference to class?

后端 未结 3 432
一生所求
一生所求 2021-01-18 17:40
class item {
public:
    item& operator=(const item &rh) {
        ...
        ...
        return *this;
    }
};

Is the following signatur

3条回答
  •  遇见更好的自我
    2021-01-18 17:51

    The signature with void would not allow chained assignments:

    a = b = c;
    

    (Look at Johannes' answer for one more example of a usage pattern based on assignment returning the assigned value.)

    That's why using such signatures is discouraged. However, if I am not mistaken, you actually can use such a signature.

提交回复
热议问题