Classes and class hierarchies
C.167
Use an operator for an operation with its conventional meaning
Reason
Readability. Convention. Reusability. Support for generic code
Example
void cout_my_class(const My_class& c) // confusing, not conventional, not generic
{
std::cout << /* class members here */;
}
std::ostream& operator<<(std::ostream& os, const my_class& c) // OK
{
return os << /* class members here */;
}
By itself, cout_my_class would be OK, but it is not usable/composable with code that relies on the << convention for output:
My_class var { /* ... */ };
// ...
cout << "var = " << var << '\n';
Note
There are strong and vigorous conventions for the meaning of most operators, such as
- comparisons (
==,!=,<,<=,>,>=, and<=>), - arithmetic operations (
+,-,*,/, and%) - access operations (
.,->, unary*, and[]) - assignment (
=)
Don't define those unconventionally and don't invent your own names for them.
Enforcement
Tricky. Requires semantic insight.