Apache C++ Standard Library User's Guide

## 38.2 Choosing a Base Class

The first question is: Which of the standard stream classes shall be the base class? The answer fully depends on the kind of addition or modification you want to make. In our case, we want to add formatting information, which depends on the stream's character type since the format string is a sequence of narrow characters. As we show later on, the format string must be expanded into a sequence of the stream's character type for use with the stream's locale. Consequently, a good choice for a base class is a specialization of `std::basic_iostream`, and since we want the format string to impact only output operations, the best choice is `std::basic_ostream`.

In general, you choose a base class by looking at the kind of addition or modification you want to make and comparing it with the characteristics of the stream classes.

- Choose the class `std::ios_base` if you add information and services that do not depend on the stream's character type.

- Choose the class template `std::basic_ios` (or a specialization thereof) if the added information does depend on the character type, or requires other information not available in `ios_base`, such as the stream buffer.

- Derive from the stream class templates `std::basic_istream`, `std::basic_ostream`, or `std::basic_iostream` if you want to add or change the input and output operations.

- Derive from the stream class templates `std::basic_{i,o}fstream`, or `std::basic_{i,o}stringstream` (or a specialization of one of these class templates) if you want to add or modify behavior that is file- or string-related, such as the way a file is opened.

Derivations from `std::basic_istream`, `std::basic_ostream`, or `std::basic_iostream` are the most common cases, because you typically want to modify or add input and output operations.

If you derive from `std::ios_base` or `std::basic_ios,` you do not inherit any input and output operations; you do this if you really want to reimplement all of them or intend to implement a completely different kind of input or output operation, such as unformatted binary input and output.

Derivations from file or string streams such as `basic_{i,o}fstream` or `basic_{i,o}stringstream` are equally rare, because they make sense only if file- or string-related data or services must be added or modified.

> NOTE -- Choose basic_istream, basic_ostream, or basic_iostream as a base class when deriving new stream classes, unless you have good reason not to do so.