Solved

ostream

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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.

706 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now