In C++ Primer, Fifth Edition, §13.1.6:
The synthesized copy-assignment operator is defined as deleted if a member has a deleted or inaccessible copy-assignment operator, or if the class has a const or reference member.
The explanation from the chapter:
Although we can assign a new value to a reference, doing so changes the value of the object to which the reference refers. If the copy-assignment operator were synthesized for such classes, the left-hand operand would continue to refer to the same object as it did before the assignment. It would not refer to the same object as the right-hand operand. Because this behavior is unlikely to be desired, the synthesized copy-assignment operator is defined as deleted if the class has a reference member.
Copying the class changes the object to which the reference member refers. Isn't this desired? Why the explanation say "is unlikely to be desired"?
Aucun commentaire:
Enregistrer un commentaire