当我想创建一个新的fstream时要继承什么?
我想用 C++ 定义一种新的 filestream
类型。我应该继承什么?
I want to define a new type of filestream
in C++. What should I inherit from?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您从头开始编写 I/O 部分,请继承
basic_filebuf
或basic_streambuf
。您可能还需要一个从basic_[i/o]fstream
派生的类,但为了方便起见,这是严格可选的。如果不需要模板,请删除basic_
并从类而不是模板继承。*stream 类都通过多态指针调度 I/O,您可以使用 rdbuf() 方法获取和设置该指针。因此,除非/直到您实现便利类,否则您可以通过实例化 std::iostream 并使用指针调用 rdbuf 来进行测试。
手头有一份标准副本对于满足派生类的要求非常有用。您的主要功能将在虚拟函数
overflow
和underflow
中。Inherit from
basic_filebuf
, orbasic_streambuf
if you're writing the I/O parts from scratch. You might also want a class derived frombasic_[i/o]fstream
, but that is strictly optional, for convenience. If templating is not required, drop thebasic_
and inherit from the classes, not the templates.The
*stream
classes all dispatch I/O through a polymorphic pointer which you can get and set using therdbuf()
method. So unless/until you implement the convenience class, you can test by instantiatingstd::iostream
and callingrdbuf
with your pointer.It is very useful to have a copy of the Standard handy, to work through the requirements for the derived class. Your main functionality will be in the virtual functions
overflow
andunderflow
.