Why iostream is good for you ?

Here is the introduction of the article "An Iostream-Compatible Socket Wrapper" from the C/C++ User Journal (December 2001) written by Maciej Sobczak. This article is really great and you should read it entirely.

Introduction of the article "An Iostream-Compatible Socket Wrapper" (CUJ Dec 2001)

When I was attending the C++ course at my university (which followed the C course), I was told that now there are better tools to do the job than before, and that, for example, the following form should no longer be used:

fprintf(stdout, "Hello World!");

Instead, the object-based approach is preferred:

cout << "Hello World!" << endl;

I know a lot of people who would argue that the second line is not a bit more object-oriented than the first; the former is even better, because it clearly states what operation is performed on what entity. What is the improvement, then? The improvement comes from code reuse and the polymorphism that the iostream-compatible objects expose. Consider this:

void printHello(ostream &stream) { stream << "Hello World!" << endl; }

printHello is reusable, because it treats its arguments polymorphically [1]. You can use it with the standard cout object, or with any other stream you can invent, whether it is connected to a file (by std::ofstream) or to a string buffer (by std::ostringstream) or to something else derived from ostream. Indeed, with a slight change you can make it work with all of the standard streams, including the wide-character versions. Beyond that, it will also work with anything else that supports operator<< and understands endl, whether the stream happens to be derived from a standard stream class or not:

template<typename T> void printHello(T &stream) { stream << "Hello World!" << endl; }

The possibilities are countless, and there is tons of code like the above to reuse. There is one minor problem, though. The standard library cannot provide all the classes needed for all I/O, because I/O programming is not limited to terminals, files, and memory devices like strings. Computers are used also for network communication where sockets are the popular abstraction. Is there any possibility to use the existing, iostream-compatible code in programs based on socket networking? Of course, there is.



Donate to the XiMoL project SourceForge.net Logo If you have any questions about XiMoL, you could write to tournois@users.sourceforge.net.