Topic: passing ifstream as a parameter


Author: dvor0054@d.umn.edu (Michael J Dvorak)
Date: 2000/06/07
Raw View
I was wondering how you can pass a ifstream as a parameter in a function.  *The ifstream is a pointer to a file to begin with right*?  *That means that it is not necessary to say something like:

read_file(ifstream &fileName)

correct*?  *Also, why is it not possible to make a ifstream as a member of a class*?  *When I tried to do this I received a error that said something like "*ANSI *C++ does not allow you to make a ifstream a part of a class."

*Thanks for the help.

Mike

--
=================================
Michael Dvorak
Mathematics Student
University of Minnesota - Duluth
USA
=================================

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Jerry Coffin <jcoffin@taeus.com>
Date: 2000/06/07
Raw View
In article <8hj3mf$8ah$1@news.d.umn.edu>, dvor0054@d.umn.edu says...
> I was wondering how you can pass a ifstream as a parameter in
> a function.

No -- to do this, basic_ifstream or one of its base classes would
have to have a copy ctor.  If there's such a requirement in the
standard, I don't see it.

> *The ifstream is a pointer to a file to begin with right*?

No.  The name ifstream is (at least normally) a typedef roughly like:

typedef basic_ifstream<char, char_traits<char> > ifstream;

There's some way for a basic_ifstream to communicate with a file in
the outside world, but most of the details are unspecified -- IIRC,
there's some language that basically says most of the actual I/O will
act roughly like you'd used putc or getc to do the actual I/O, but
that's about it.

> *That means that it is not necessary to say something like:
>
> read_file(ifstream &fileName)
>
> correct*?

If you want to use a ifstream (or any of the other iostreams) in a
function, then passing by reference is usually the right thing to do.

> *Also, why is it not possible to make a ifstream as a member of a
> class*?  *When I tried to do this I received a error that said
> something like "*ANSI *C++ does not allow you to make a ifstream
> a part of a class."

I've never heard of such a prohibition, and doing some looking I
can't anything in the standard that implies that there should be any
problem with a stream being a member.  I've certainly written classes
that had various objects in the iostream heirarchy as members with no
problems at all.  What compiler are you using?

--
    Later,
    Jerry.

The universe is a figment of its own imagination.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]