[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

ostream

Posted on 2003-11-13
3
Medium Priority
?
431 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 80 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 80 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.
Suggested Courses

656 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