The basic stream library (iostream) is designed to resolve most of the general problems you may run into during your programming endeavors. However, sometimes it simply isn't enough, and you need to extend it to suit your application—your special situation. This article will show you how to do that.
The key area in the streams is the buffer area that is included in the streambuff class. Most of the time this function works in the background, accomplishing tasks that are a little more complex. Perceive this as an internal class that does the heavy work, while the rest of the classes that you declare when working with streams are just specializations of this one.
For a start, let's see how the buffer area works and how it is constructed to assure an efficient build up of the streams. Before we move on, let me specify that although this article is part of my series about streams, it will be a little more advanced. At least a good knowledge of C and a strong OOP background are required if you aim to completely understand and grasp my words.
This content of this article may not even be useful to you unless you care about a specific mode for performance. The polymorphic power of C++ gives you the possibility to use it in a multitude of ways, and you may find a different approach paired with a good solution using the already-defined streams. Nevertheless, this article will give you a greater insight into how it works behind the scene. If you are interested, just read on and be amazed.
Read my introduction to streams; once you're done we can move on. As mentioned during the previous article of this series (Basic I/O), the ofstream, ifstream are just typedefs of the basic_isftream/basic_ofstream with the template parameter "char." Those were made to attract newcomers to the library and so that you will write less.
So in fact, this brings us to the point that we can declare the basic_ifstream to any type: for example, on a Car Type. The compiler might not even complain about it (this is subject to how that specific class is constructed). However, once you start calling functions around, serious errors will pop up.
The problem is simply that there is no trait_type structure declared for a Car Type. In the basic package of the library, the C++ programming guide defined only the wide and narrow characters. This you will find in the iosfwd file input. Writing a specialization for the Car Type can also be troublesome; it will prove to be simpler to go even further in the derivation list of the basic_ifstream and find that the road leads to the filebuffer.