Link to home
Start Free TrialLog in
Avatar of ankuratvb
ankuratvbFlag for United States of America

asked on

ostream reference to ofstream(or ostream) object

Hi,

I have a global reference to an ofstream object(which i can change to ostream if reqd)

ofstream fcout;

In a fn.,i want to set the value of this global reference i.e.

void func(ostream &fout)
{
 fcout=fout;//this doesnt work.How to do this part?
}

I tried ostream &fcout;

That too didnt work.
Avatar of Karl Heinz Kremer
Karl Heinz Kremer
Flag of United States of America image

The C++ standard does not allow copy constructors or assignment operators for streams. This is the reason why your code does not work. You would run into problems when one of the streams would get destroyed. What should then happen to the other stream?

To get around your problems, define a pointer to an ofstream and initialize that:

ofstream *pFcout = NULL;

void func(ostream &fout)
{
    pFcout = &fout;
}

Whenever you use the ofstream, make sure that you dereference the pointer:
*pFcout

Avatar of ankuratvb

ASKER

> You would run into problems when one of the streams would get destroyed. What >should then happen to the other stream?

The same that happens in JAVA.the other stream object should keep referring to the stream
it was.


C++ is not Java. (But you probably already know that :-)
The standard committee decided to not support this feature. Some compilers (MS) still do allow this for backward compatibility reasons, but all other modern compilers will probably flag this as an error.
Hi khkremer,

I know c++ is not java. :~)

I thought u asked me a logical question so i gave u a logical answer which i know is not supported by c++.
We all know that the std. c++ library has some shortcomings and we have to live with it.

I tried ur suggestion but it gives me an error:

Cannot convert from ostream * to ofstream *.
on this line:pFcout = &fout;

Any other suggestions?
ASKER CERTIFIED SOLUTION
Avatar of Karl Heinz Kremer
Karl Heinz Kremer
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The discussion does the reverse of what i want.To set a local stream according to a global stream is done for me.

This is what i want to do.

I have a global stream:

ofstream fcout;

In a fn.,i want to set the value of this global reference i.e.

void func(ostream &fout)
{
fcout=fout;//this doesnt work.How to do this part?
}
so that in my other functions say func1()
i dont have to pass the reference to the stream
thus in func1(),i can do this:

void func1()
{
fcout<<"result";
}

One possible solution is passing the stream reference to evry function but that is my last resort.

I'll try the casting bit.
Hi khkremer,

The global reference thing and the casting bit as well sounded a bit dirty to me.
So,i decided to send a ostream reference to all my functions and now it works fine.

Thanx for your help.
That's how I would have done it.