Topic: Passing File Variables (Doesn't Work Always For Me)


Author: "Larry Brasfield" <larrybr@earthlink.net>
Date: 1997/12/16
Raw View
Christopher M Lusardi <lusardi@acsu.buffalo.edu> wrote in article
<671e85$10c$1@prometheus.acsu.buffalo.edu>...
>
> // Why does the below file passing techniques work in some files but not in others?
. . .
> void my_class::test_file_passing (ifstream fin,ifstream fin2)

I think it would be better if ifstream and ofstream had no
copy constructors.  That way, when somebody makes the mistake
of passing such objects by value instead of by reference,
they would get a compile-time error instead of a mysterious
failure.

Your problems will vanish as soon as you put a '&' after each
'ifstream' in your method signatures.  If you think about it,
having several ifstream objects all referring to the same
source stream is strange, especially if they are allowed to
do any kind of buffering.  Pass them by reference to avoid
the whole issue and promote efficiency.


--
-- Larry Brasfield
The aforementioned views are mine alone.
(Convert under to dot for e-mail reply.)
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: "Bill Wade" <bill.wade@stoner.com>
Date: 1997/12/16
Raw View
Christopher M Lusardi <lusardi@acsu.buffalo.edu> wrote in article
<671e85$10c$1@prometheus.acsu.buffalo.edu>...
> X-No-Archive: yes
>
> // Why does the below file passing techniques work in some files but not
in others?
> // I do create the files in another method. This posted code works fine!

I don't believe the standard provides a copy constructor for ifstream.  It
is possible that your implementation is providing a copy constructor that
isn't doing what you expect.

Consider passing iostreams by reference.
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]