Solved

ostream reference to ofstream(or ostream) object

Posted on 2004-04-04
8
1,259 Views
Last Modified: 2007-12-19
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.
0
Comment
Question by:ankuratvb
  • 4
  • 4
8 Comments
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
Comment Utility
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

0
 
LVL 9

Author Comment

by:ankuratvb
Comment Utility
> 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.


0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
Comment Utility
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.
0
 
LVL 9

Author Comment

by:ankuratvb
Comment Utility
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?
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 100 total points
Comment Utility
I did not notice the ostream vs. ofstream in your original question. The compiler is right :-) You could get around this (we are after all only using pointers, and not the real objects, so problems like slicing - or truncating - are not an issue) by casting to ofstream *:
pFcout = (ofstream *) &fout;

But, I suspect what you want to do can be done with other tools. Check this article to see if it describes your problem (and the solution for it): http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=5d4ted%242ki%241%40news.belwue.de

0
 
LVL 9

Author Comment

by:ankuratvb
Comment Utility
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.
0
 
LVL 9

Author Comment

by:ankuratvb
Comment Utility
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.
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
Comment Utility
That's how I would have done it.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now