简体   繁体   中英

Why does copy assignment operator return *this?

The copy assignment operator should just let the LHS equal the RHS.

Why does it return *this?

This supports chained assignment:

a = b = c = 0;

This assigns 0 to each of a, b, and c.

Returning a (non-const) reference instead of a value supports things like:

(a = b) = 0;

Which is also allowed, though it's not very common.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM