Solved

ostream

Posted on 2003-11-13
3
427 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

713 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