Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

ostream reference to ofstream(or ostream) object

Posted on 2004-04-04
8
1,295 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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 100 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

839 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