This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Redirecting I/O


(This question probably should be on gcc-help instead of this list.)

> I'm trying to redirect I/O in my C++ application and am having some
> difficulty. I am trying to use cout or a file for output based on some
> condition. cout is an ostream object and file is an ofstream object.
> The types are incompatible, as in:
>
> bool condition;
> ofstream x;
> ofstream out = (condition)?cout: x; ? // won't work because of cout

Try:

ostream& out = condition ? cout : x;

> In addition I would like to redirect an ofstream object to be the same as
> out, as in;
>
> void fn() { object = out; } // won't work because '=' is private.

Again, try declaring object as "ostream&".

-cary


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]