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 15 we learned that additional data can be added to a stream object by using xalloc(), iword(), and 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 15.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.21 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:
date today; odatstream ostr(cout); // ... ostr << setfmt("%D") << today;
In the next sections, we explore how we can implement such a derived stream type.
OEM Edition, ©Copyright 1999, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.