Topic: C++ Input/Output File Streams
Author: Michiel.Salters@cmg.nl (Michiel Salters)
Date: Mon, 18 Aug 2003 14:54:06 +0000 (UTC) Raw View
nephesys@hotmail.com (Lee) wrote in message news:<36e3540e.0308160639.b37e773@posting.google.com>...
> Why do I/O file streams in c++ must have to be passed as reference
> parameters, instead of value parameters?
Because value parameters are copied, and streams can't be copied.
To answer the next question: Streams can't be copied, because
1) they're typically used polymorphically (ostream& to pass an
ofstream) and
2) Defining semantics would be complex, even for a single stream type
(Is a copy of an ofstream a distinct file? Is a copy of a console
window stream a distinct window?)
HTH,
--
Michiel Salters
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: jdennett@acm.org (James Dennett)
Date: Mon, 18 Aug 2003 19:01:33 +0000 (UTC) Raw View
Lee wrote:
> Why do I/O file streams in c++ must have to be passed as reference
> parameters, instead of value parameters?
C++ streams are not copyable, because (a) the semantics of making
a copy of a stream are hard to pin down in general, (b) pinning
them down might restrict some uses of streams, and (c) there is no
need to make a copy of a stream just to be able to use it, when
pass by reference is a clearer reflection of what you are trying
to do: tell a piece of code which stream to use. One problem with
attempting to define copying for streams is that they use streambufs,
and allowing the stream to be copied would give issues of whether to
try to make the streambufs copyable or to deal with shared ownership
semantics or other lifetime issues.
-- James
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: francis@robinton.demon.co.uk (Francis Glassborow)
Date: Mon, 18 Aug 2003 19:01:39 +0000 (UTC) Raw View
In article <36e3540e.0308160639.b37e773@posting.google.com>, Lee
<nephesys@hotmail.com> writes
>Why do I/O file streams in c++ must have to be passed as reference
>parameters, instead of value parameters?
You will get the same answers here as you got in comp.lang.c++.moderated
(w/o the comments about shouting) because the answers you got there are
the correct ones.
--
Francis Glassborow ACCU
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: chris@bubblescope.net (chris)
Date: Mon, 18 Aug 2003 19:01:44 +0000 (UTC) Raw View
Lee wrote:
> Why do I/O file streams in c++ must have to be passed as reference
> parameters, instead of value parameters?
>
The basic reason is that value parameters create a new copy of the
object. If you think about it, what would it mean if you have 2 or 3
copies of "cout" lying around? Also file streams have various parameters
(like EOF, finished, error) which if you could have multiple copies
would have to somehow be syncronised. This would (I expect) have to be
done by each copy having an internal reference to some master copy. If
you are going to do that, why not just pass by reference in the first
place? :)
Chris
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: nephesys@hotmail.com (Lee)
Date: Sun, 17 Aug 2003 18:59:07 +0000 (UTC) Raw View
Why do I/O file streams in c++ must have to be passed as reference
parameters, instead of value parameters?
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]