Figure 15.1. Class Hierarchy of the Fundamental Stream Classes
The classes in this class hierarchy play the following roles:
• The base class ios_base defines the properties of all stream classes independent of the char-
acter type and the corresponding character traits. Most of this class consists of components and
functions for state and format flags.
• The class template basic_ios is derived from ios_base and defines the common prop-
erties of all stream classes that depend on the character types and the corresponding character
traits. These properties include the definition of the buffer used by the stream. The buffer is an object of a class derived from the template class basic_streambuf with the corresponding template instantiation. It performs the actual reading and/or writing.
• The class templates basic_istream and basic_ostream derive virtually from
basic_ios and define objects that can be used for reading or writing, respectively. Like
basic_ios, these classes are templates that are parametrized with a character type and its traits. When internationalization does not matter, the corresponding instantiations for the char- acter type char — istream and ostream — are used.
• The class template basic_iostream derives from both basic_istream and
basic_ostream. This class template defines objects that can be used for both reading and
writing.
• The class template basic_streambuf is the heart of the IOStream library. This class
defines the interface to all representations that can be written to or read from by streams and is
used by the other stream classes to perform the reading and writing of characters. For access to some external representation, classes are derived from basic_streambuf. See the following subsection for details.
Purpose of the Stream Buffer Classes
The IOStream library is designed with a rigid separation of responsibilities. The classes derived from basic_ios handle only formatting of the data.2 The reading and writing of characters is performed by the stream buffers maintained by the basic_ios subobjects. The stream buffers supply character buffers for reading and writing. In addition, an abstraction from the external representation, such as files or strings, is formed by the stream buffers.
Thus, stream buffers play an important role when performing I/O with new external represen- tations (such as sockets or graphical user interface components), redirecting streams, or combining streams to form pipelines (for example, to compress output before writing to another stream). Also, the stream buffer synchronizes the I/O when doing simultaneous I/O on the same external represen- tation. The details about these techniques are explained in Section 15.12, page 819.
By using stream buffers, it is quite easy to define access to a new “external representation,” such as a new storage device. All that has to be done is to derive a new stream buffer class from basic_streambuf or an appropriate specialization and to define functions for reading and/or writing characters for this new external representation. All options for formatted I/O are available automatically if a stream object is initialized to use an object of the new stream buffer class. See Section 15.13, page 826, for details of stream buffers and Section 15.13.3, page 832, for examples of how to define new stream buffers for access to special storage devices.
Detailed Class Definitions
Like all class templates in the IOStream library, the class template basic_ios is parametrized by two arguments and is defined as follows:
namespace std {
template
class basic_ios;
}
The template arguments are the character type used by the stream classes and a class describing the traits of the character type that are used by the stream classes.