I'm currently learning C++ and I wanted to overload an operator for the following class, that should count the amount of a specific character in various strings:
I'm in two minds about this, but as it's on my mind:
While it's possible to overload C++ operators in all sorts of ways, by convention (or even rule) that you should preserve the natural semantics for overloaded operators (see "C++ Coding Standard", Sutter & Alexandresu, rule 26).
When it comes to operator<<, this convention means you should only really this operator in two situtations:
1. when you're shifting something left (following the lead of the built-in bitwise left-shift operator)
2. when your inserting into a sequential stream, like ostream.
Using operator<< to aggregate counts does not really fit into either of these categories, so your original count method is probably the better solution. It would even better if the method name made it clear that it also aggregates.