Solved

ostream

Posted on 2003-11-13
3
426 Views
Last Modified: 2010-04-01
To C++ experts,

    If I want my print() function to be able to print things both on files and screen, is that better to declare the funtion as "void print(ostream &os)"
or "void print(ostream os)" ? or any other format ? and why ?

    thanks.

meow.
0
Comment
Question by:meow00
  • 2
3 Comments
 
LVL 15

Accepted Solution

by:
efn earned 20 total points
ID: 9743806
Passing the stream by reference as in

void print(ostream & os)

is preferable.  Stream objects are not designed to be passed around by value, so you should only have one stream object and pass it around by reference.

A stream object needs to maintain state, and the state changes when you output to the stream.  It may be buffering the output or maintaining a file position.  If you pass the stream by value, the print function will change the state of a temporary copy of the stream object, so the original stream object will be confused by operations going on behind its back.

--efn
0
 
LVL 30

Assisted Solution

by:Axter
Axter earned 20 total points
ID: 9744832
According to the C++ standard, you're not allowed to pass a stream object by value.

A few compilers, like VC++ 6.0 will let you compile this type of code, but will then crash at run time.

So never pass a stream object by value.  It will either fail to compiler, or crash.
0
 
LVL 30

Expert Comment

by:Axter
ID: 9744881
FYI:

Section 27.4.4 of C++ Standard:
private:
basic_ios(const basic_ios& ); // not defined
basic_ios& operator=(const basic_ios&); // not defined

basic_istringstream derives from basic_ios via the following inheritance.

basic_istringstream->basic_istream->basic_ios

The above section of the C++ standard shows the copy constructor and assignment operator as private and not defined.

That means any derived class like ostream can not be pass by value.
In a compliant implementation, you'll get a compile error if you try to pass by value.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C++ standard library based binary archive format 6 104
thread-safe code in c++ 2 115
Dynamically allocate memory 9 62
designing in object programming 12 94
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question