Hi everyone,
I want to overload all <<operators in a class which inherits from ostream (to do some action before and/or after printing). What is the best way to do this?
I already tried to overload _M_insert (see line 366 in ostream: http://gcc.gnu.org/onlinedocs/gcc-4.6.3/libstdc++/api/a00967_source.html )
Are you sure you need to inherit ostream? If you are making a console wouldn't composition make more sense? At any rate maybe something like template<typename _ValueT> Console& Console::operator<<(Console& fcout, _ValueT __v)
_M_insert is an implementation detail of your library, it's not a part of the streams interface.
ostream's operator<<'s aren't virtual (and many of them aren't even members), so any function that deals with your stream through std::ostream& will use the original operator<<'s.
What is your intended use case? If it's just something like
1 2
mystream s(sink);
s << "example: " << n << '\n';
you could store std::ostream& as a member of mystream and provide one non-member template operator<< for mystreams, perhaps with specializations as necessary.
For complex I/O streams filtering, take a look at boost.iostreams
It is not a good/smart idea to create/have a class inheriting from ostream, instead your class should contain a function(an overloaded function, need not to be a member of your class, it might be a friend function, or just a function which use public method of your class) which wraps and returns the result to the caller