Some operators cannot be overloaded as class members. One such example are the bitwise shift operators used for streams see here for example. The reason for that (as far as I understand it) seems to be that the left hand site operand is the one you define the behaviour for and you cannot define behaviour for the built-in streams. Please correct me if I'm wrong.
I think it would be useful to define those overloads as class members, because they likely need to access private data. Sure, there's friend
, but that seems to be more of a workaround.
Now this is more of a hypothetical question, but why not swap the meaning of >>
and <<
then?
obj >> std::cout;
obj << std::cin;
This way, the whole operation happens from the point of view of the object, not the stream. The overload can be made as a member function, because the object is the left hand operand. Would that be possible?
I guess there are a lot of things this would break, like readability for starters as everybody is used to the other way. What other things would this break?