Previous fileTop of DocumentContentsIndex pageNext file
Apache C++ Standard Library User's Guide

38.1 Deriving a New Stream Type

Sometimes it is useful to derive a stream type from the standard iostreams. This is the case when you want to add data members or functions, or modify the behavior of a stream's I/O operations.

In Chapter 36 we learned that additional data can be added to a stream object by using std::os_base::xalloc(), std::ios_base::iword(), and std::ios_base::pword(). However, this solution has a certain weakness in that only a pointer to the additional data can be stored and someone else has to worry about the actual memory.

This weakness can be overcome by deriving a new stream type that stores the additional data as a data member. Let's consider again the example of the date inserter and the setfmt manipulator from Section 36.3. Here let's derive a new stream that has an additional data member for storing the format string together with a corresponding member function for setting the date format specification. [This, of course, is only an example. You would probably never derive a new class for adding only one data member. However, it keeps the example simple and allows us to demonstrate the principle of deriving new stream classes.]

Again, we confine the example to the inserter of the date object and omit the extractor. Instead of inserting into an output stream, as we did before, we now use a new type of stream called odatstream:

In the next sections, we explore how we can implement such a derived stream type.



Previous fileTop of DocumentContentsIndex pageNext file