C++ why the assignment operator should return a const ref in order to avoid (a=b)=c

后端 未结 5 1435
情书的邮戳
情书的邮戳 2020-11-30 04:16

I am reading a book about C++ and more precisely about the operator overloading.

The example is the following:

const Array &Array::operator=(cons         


        
5条回答
  •  佛祖请我去吃肉
    2020-11-30 04:42

    There is no need to avoid this, unless the book is aimed at programmers that commonly write (x=y)=z when they mean x=y=z. In practice, nobody in their right mind writes that, so the precaution is entirely unnecessary. It also forbids some other terse constructs, such as (x=y).nonConstMember(), that hardly anyone writes but that might be useful in some contexts (although they shouldn't be over-used).

    @ybungalobill is right, get a better book.

提交回复
热议问题