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

32.3 A Simple Extractor and Inserter for the Example

Following the read and write conventions of iostreams, we would insert and extract the date object like this:

or

For the next step, we would implement shift operators as inserters and extractors for date objects. Here is an extractor for class date:

//1The returned value for extractors (and inserters) is a reference to the stream, so that several extractions can be done in one expression.
//2The first parameter usually is the stream from which the data shall be extracted.
//3The second parameter is a reference, or alternatively a pointer, to an object of the user-defined type.
//4In order to allow access to private data of the date class, the extractor must be declared as a friend function in class date.
//5The return value is the stream from which the data was extracted.

As the extractor accesses private data members of class date, it must be declared as a friend function to class date. The same holds for the inserter. Here's a more complete version of class date:

The inserter can be built analogously, as shown below. The only difference is that you would hand over a constant reference to a date object, because the inserter is not supposed to modify the object it prints.



Previous fileTop of DocumentContentsIndex pageNext file