Solved

ostream

Posted on 2003-11-13
3
428 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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
maze travler 6 72
Template syntax for variable length arrays 9 78
FMX StringGrid1->Canvas->FillRect Problem 3 218
draw a Christmas tree by using a nested loop? 26 102
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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

737 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