55. Prefer the canonical form of assignment

Summary

Your assignment: When implementing operator=, prefer the canonical form—nonvirtual and with a specific signature.

Discussion

Prefer to declare copy assignment for a type T with one of the following signatures (see [Stroustrup00] and [Alexandrescu03a]):

image

Settle for the second version if you would need to make a copy of the argument inside your operator anyway, such as the swap-based idiom featured in Item 56.

Avoid making any assignment operator virtual (see [Meyers96] §33 and [Sutter04] §19). If you think you need virtual behavior for assignment, reread those citations first. If that doesn’t ...

Get C++ Coding Standards: 101 Rules, Guidelines, and Best Practices now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.