My view on the whole printf vs. cout thing is that stream.h provides derivable
classes. I started off learning C++ by designing classes for my core
application processing and leaving the I/O to stidio based functions. After
a bit I got into using the stream stuff and its a hell of a lot nicer in a C++
context. Being able to overide operators << and >> allows you to populate
objects or output their contents to the stream in a very neat way. You
couldn't do this as nicely with printf or any such stdio functions. The
other aspect to the streams classes that nobody has mentioned, is the ability
to capture exceptions. If you are going to need to do this then using
streams is a lot better than stdio.
For instance, deriving a class from the ifstream and a field parsing class
will give you the ability to read in a file and parse it from your derived
class. Its more awkward doing this using stdio and you have to come out of
the modular class structure to do that.
Using malloc instead of new is a great way to ensure that your base object
won't be instntiated if you're declaring a derived object. Only new will do
this for you. As said previously, you can override new to do what you wish
also. The opposite goes for delete.
Thats my 2 cents.
Kenn Humborg <kenn at research.wombat.ie> wrote:
On Sat, Nov 27, 1999 at 04:54:04PM +0000, kevin lyda wrote:
> Mel wrote:
> > cout instead of ifstream
> > fstream instead of fopen
> > new instead of malloc
>> i take this as libc's stdio vs. c++'s stl streams. (i'm not a c++
> person, but my understanding is that there used to be a c++ lib -
> #include <streams.h> for example - and now there's the stl with #include
> <streams>) one argument might be efficiency. stdio has been studied a
> great deal and the idea has been to reduce data copying. now how much
> studying has been put into making the stl efficient? dunno.
You are mixing up the C++ Standard Library (the analog of the
standard header files in ANSI C providing I/O, string support,
and other stuff) and the Standard Template Library (STL) which
provides generic container, iterator and algorithm classes
such as lists, queues, maps, sorts, and other stuff. STL
uses C++ templates to provide type-safe support for any data
C++ I/O streams are nothing to do with STL.
Mind you, as far as I'm concerned, << means left shift,
not "format in some hard-to-specify way for shoving up
an ostream". But that's just a personal taste thing...
And the difference between #include <streams.h> and
#include <streams> is that the .h version puts the
definitions into the global namespace, whereas the
newer non-.h version puts them in the std:: namespace.
So if you do a #include <streams>, you'll have to do
cout << "hello, world\n";
std::cout << "hello, world\n";
Irish Linux Users' Group: ilug at linux.iehttp://www.linux.ie/mailman/listinfo/ilug for (un)subscription information.
List maintainer: listmaster at linux.ie
Get free email and a permanent address at http://www.netaddress.com/?N=1
Maintained by the ILUG website team. The aim of Linux.ie is to
support and help commercial and private users of Linux in Ireland. You can
display ILUG news in your own webpages, read backend
information to find out how. Networking services kindly provided by HEAnet, server kindly donated by
Dell. Linux is a trademark of Linus Torvalds,
used with permission. No penguins were harmed in the production or maintenance
of this highly praised website. Looking for the
Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!