Yes. You can overload the thread insert statement as a non-member function. The disadvantage, of course, is that you cannot make the function a friend (which is often done), so you won’t be able to output anything that was not opened by the class through a public accessor, but you are limited in that no matter what you do, if you cannot change the class.
Example:
class Foo {
public:
std::string name() const;
int number() const;
private:
};
std::ostream& operator<< (std::ostream& os, const Foo& foo) {
os << "(" << foo.name() << "," << foo.number() << ")";
return os;
}
Foo foo;
std::out << foo;
source
share