Click Here!
home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
[an error occurred while processing this directive]
Previous Table of Contents Next


Note that the last comma is not there. Most programmers put one there by habit, and the designer (me, in this case) figured that it would be best to leave that habit in place. The compiler will generate an error if the semicolon is left out when the macro is used in code.

The preceding example does not take into account any arguments that must be passed to the stream constructor. In your code, this macro would be used like this:

// Code above
SYNCED_IOSTREAM_CLASS( ostream, myOstream ) ;
// code below

If the source library with the class can be edited, it might be worthwhile to make the code optional, or allow it to be enabled by using a #define statement.

14.2 Make my own classes compatible with cin and cout?

Problem

It would substantially simplify code to use the istream and ostream operators >> and << to perform the custom output, rather than to individually output the public data members each time, or to output protected and private members via member access functions.

Technique

After a class is made compatible with the standard istream and ostream classes, I/O for the class will be much more consistent. The best way is to overload the << or >> operators for your class.

The extraction and insertion operators for C++ take the following format:

For output:

ostream& operator<<
(ostream& outstream, <insert your data type here>& data)

For input:

istream& operator>>
(istream& instream, <insert your data type here>& data)

The code in these functions is dependent on the contents of your data type. For classes, it might be necessary to declare the function a friend of the class to allow it access to private and protected data members.

Steps

1.  Determine the best way to get data into or out of your class—either by the friend declaration or some other means, such as an overloaded = operator.
2.  Create the declaration of the function. Be sure the iostream.h header file is included before the declaration, or the ostream or istream data types will not be visible and result in a compile-time error.
The easy way is to make the overloaded operators for your class friends to your class if you have to handle protected or private data members. However, this is not recommended because it circumvents any validity checking imposed by the functions that would validate this input. With some modifications, it is possible to make this reasonably safe, but the safest (the fewest possible side-effects) way is to not use the friend mechanism unless you absolutely have to.
You can use the following code as a template (it’s taken from the iostreams\ex3 directory on the CD-ROM):
istream& operator>>(istream&, RowProcessor&);
ostream& operator<<(ostream&, RowProcessor&);
3.  Handle the data input and output. When processing is finished, return the istream or ostream object.

How It Works

Using the operator overloading mechanism, the C++ streams libraries “chain” what are in essence procedure calls for stream operations. Because the overloading mechanism depends entirely on the argument types passed to a function, this system is easily extendable for user-defined types.

Comments

Because file handling in the streams library is also based on the same mechanism, extending a class for console I/O will automatically support streaming to files and character arrays as well.

14.3 Perform complex formatting with cout or another ostream object?

Problem

Although formatting with C++ is simpler than formatting with the printf family of functions from the C stdio library, it still has its pitfalls. On some systems, a lot of in-stream formatting needs to have an additional header file included to give access to the additional member functions used to perform advanced formatting.

Technique

In-stream formatting operators tend to be underrated, and unlike the printf series of functions, are type-safe and easily extended to fit the needs of your programs. Although some of the functionality is visible with the stock header file, iostream.h, most of the functionality of the stream modifiers is in iomanip.h.

Extending the system with your own manipulators is rather easy as well, and will be covered later. Normally, they will work with string-based and file-based streams as well.

The predefined manipulator library is by no means complete, however, and only encompasses some of the actual functionality included in the streams classes. Additional functionality can be accessed through each stream’s setf() function and by passing the appropriate constant to it.

Standard Manipulators

Explaining standard manipulators is best done with a sample program. There are many possibilities for combining standard manipulators, and others might have been added in your development environment that are specific to your compiler vendor. Check your documentation for any of the nonstandard manipulators.

Setting Output Width with setw()

The setw() manipulator takes an integer argument and defines the default width of each item given to the output stream. The manipulator is in effect until changed again, and setting it to 0 will reset it to defaults. Using this manipulator will not cause output to be truncated; if the width of the item sent into the stream is wider than the given setw() value, the width of the item will determine the space given to the item.

The code

cout << “double d1 = “ << setw(9) <<  d1 << ‘.’ << endl ;


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.