I want to define MyOStream which inherits publicly from std::ostream. Let's say I want to implement my own ofstream.
How can this be done? I'll be glad for any help, coded开发者_如何转开发 example or any relevant link...
thanks!
I don't understand exactly what you're trying to accomplish here. User code shouldn't inherit from the streams themselves, as the streams are intended to provide a generalized locale specific conversion/"stringizing" facility. If you're trying to use an ostream
which can write to a new buffer location (i.e. a gzip stream), then one should generally inherit from basic_streambuf
instead, which allows you to use the existing iostream conversion facilities but will allow you to redirect their input/output.
If you want to learn the ins and outs of how iostream itself operates, the best book I've heard about the subject is Standard C++ IOStreams and Locales by Angelika Langer and Klaus Kreft. I can't myself vouch for the book because I have yet to get my copy (it is next on my list), but you can find several recommendations for it here on StackOverflow.
You also probably want to take a peek at boost::iostreams, which provides some helpers for anyone wishing to customize the behavior of the iostream system.
精彩评论