Use Definitions That Are Consistent with the Built-in Meaning

When you design a class, you should always think first about what operations the class will provide. Only after you know what operations are needed should you think about whether to define each operation as an ordinary function or as an overloaded operator. Those operations with a logical mapping to an operator are good candidates for defining as overloaded operators:

• If the class does IO, define the shift operators to be consistent with how IO is done for the built-in types.

• If the class has an operation to test for equality, define operator==. If the class has operator==, it should usually have operator!= as well.

• If the class has a single, natural ordering operation, define ...

Get C++ Primer, Fifth Edition 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.