Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ostream reference to ofstream(or ostream) object

Posted on 2004-04-04
8
Medium Priority
?
1,393 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
ID: 10752265
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
ID: 10752285
> 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
ID: 10752359
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Author Comment

by:ankuratvb
ID: 10757060
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
 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 400 total points
ID: 10757969
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
ID: 10758246
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
ID: 10800121
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
ID: 10800429
That's how I would have done it.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…
Suggested Courses

927 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