继承std :: ostream [英] inherit std::ostream

查看:665
本文介绍了继承std :: ostream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想定义从std :: ostream公开继承的MyOStream。假设我想实现自己的ofstream。

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...

谢谢!

推荐答案

我不明白你在这里想要完成什么。用户代码不应从流本身继承,因为流旨在提供通用的特定于语言环境的转换/字符串化工具。如果您尝试使用可以写入新缓冲区位置(即gzip流)的 ostream ,那么通常应该从 basic_streambuf继承相反,它允许您使用现有的iostream转换工具,但允许您重定向其输入/输出。

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.

如果您想学习关于iostream本身如何运作的细节,我听过的关于这个主题的最好的书是标准C ++由Angelika Langer和Klaus Kreft创作的IOStreams和Locales 。我不能自己担保这本书,因为我还没有得到我的副本( 在我的列表旁边),但你可以在StackOverflow上找到一些建议。

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.

您可能还想看一眼 boost :: iostreams ,为希望自定义iostream系统行为的人提供一些帮助。

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.

这篇关于继承std :: ostream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆