Why does operator = return *this?

后端 未结 5 2289
感动是毒
感动是毒 2021-02-19 21:26

Say I want to override the operator = so I can do something like

Poly p1;  // an object representing a polynomial
Poly p2;  // another object of the         


        
5条回答
  •  长发绾君心
    2021-02-19 21:52

    Returning a reference to the target object allow assignment chaining (cascading), and overloading operators within a class follows right-associative (click here for detailed operator overloading rules)

    Poly a, b, c;
    a = b = c;
    

提交回复
热议问题