简体   繁体   English

继承std :: ostream

[英]inherit std::ostream

I want to define MyOStream which inherits publicly from std::ostream. 我想定义从std :: ostream公开继承的MyOStream。 Let's say I want to implement my own ofstream. 假设我想实现自己的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 (ie 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. 如果您正在尝试使用可以写入新缓冲区位置的ostream (即gzip流),那么通常应该继承自basic_streambuf ,这允许您使用现有的iostream转换工具,但允许您重定向输入输出。

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. 如果你想了解iostream本身如何运作的细节,我听过的关于这个主题的最好的书是Angelika Langer和Klaus Kreft的标准C ++ IOStreams和Locales 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. 我无法为这本书担保,因为我还没有得到我的副本(它我的列表中的下一个),但你可以在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. 您可能还想看看boost :: iostreams ,它为希望自定义iostream系统行为的人提供了一些帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM