Go to the documentation of this file.
00001 /** \page ios Why iostream is good for you ?
00003 Here is the introduction of the article "An Iostream-Compatible Socket Wrapper" 
00004 from the C/C++ User Journal (December 2001) written by Maciej Sobczak. 
00005 This article is really great and you should read it entirely.
00007 \section article Introduction of the article "An Iostream-Compatible Socket Wrapper" (CUJ Dec 2001)
00009 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:
00011 \code
00012 fprintf(stdout, "Hello World!");
00013 \endcode
00015 Instead, the object-based approach is preferred:
00017 \code
00018 cout << "Hello World!" << endl;
00019 \endcode
00021 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:
00023 \code
00024 void printHello(ostream &stream)
00025 {
00026     stream << "Hello World!" << endl;
00027 }
00028 \endcode
00030 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:
00032 \code
00033 template<typename T>
00034 void printHello(T &stream)
00035 {
00036     stream << "Hello World!" << endl;
00037 }
00038 \endcode
00040 The possibilities are countless, and there is tons of code like the above to reuse. 
00041 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. 
00042 Computers are used also for network communication where sockets are the popular abstraction. 
00043 Is there any possibility to use the existing, iostream-compatible code in programs based on socket networking? Of course, there is.
00045 */

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