Solved

ostream reference to ofstream(or ostream) object

Posted on 2004-04-04
8
1,322 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
[X]
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
  • 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 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Writing a parser for java language 4 103
How can i compile this github project?? 2 104
Embarcadero C++ builder XE10.1 Berlin TRegistry declaration 1 60
learn programming 8 95
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

759 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