c++11 - std::ostream to file or standard output -


If a file name is available or on screen (stdout) otherwise I want to write my output in a file, I have read the post on the forum and I got a code, wrapped in a method below:

  std :: shared_ptr  gt; {of.open (fname); Buf = of.rdbuf (); } Else buf = std :: cout.rdbuf (); Std :: shared_ptr & lt; Std :: ostream & gt; P (new study :: ostream (buff)); Return p; }  

The code works perfectly when using in-place, unfortunately it behaves strangely when wrapped in a different method (given above). Is it because because after the call is over, the object defined within the method (or fond) is destroyed?

I am using this part of the code in many places and it should actually be removed as a separate non-repeating piece: a method or a class. How can I get this?

You are right that the problems that you are coming from destruction are due to of Will there be nothing like this (involuntary) work?

  std :: shared_ptr & lt; Std :: ostream & gt; Out_stream (const std :: string and fname) {if (fname.length ()> 0) std :: share_ptr & lt; Std :: ostream & gt; P (new standard :: offream (FNN)); Else std :: shared_ptr & lt; Std :: ostream & gt; P (new standard :: ostream (std :: cout.rdbuf ()); }  

Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

lua - HowTo create a fuel bar -