Paul Manta
Paul Manta

Reputation: 31577

What to inherit from when I want to create a new fstream?

I want to define a new type of filestream in C++. What should I inherit from?

Upvotes: 0

Views: 95

Answers (1)

Potatoswatter
Potatoswatter

Reputation: 137830

Inherit from basic_filebuf, or basic_streambuf if you're writing the I/O parts from scratch. You might also want a class derived from basic_[i/o]fstream, but that is strictly optional, for convenience. If templating is not required, drop the basic_ 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 the rdbuf() method. So unless/until you implement the convenience class, you can test by instantiating std::iostream and calling rdbuf 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 and underflow.

Upvotes: 4

Related Questions