copy of stream

P

Philipp

Hello,
How can you copy a stream (or change a reference to it)?

Imagine a function which writes "Hello" to a filestream:

void SomeClass::write(ofstream& originalStream){
ofstream newStream;
newStream = originalStream; // this does not work
newStream << "Hello" << endl;
}

How can I make that right? Thanks for your answers Phil
 
J

JKop

Philipp posted:
Hello,
How can you copy a stream (or change a reference to it)?

Imagine a function which writes "Hello" to a filestream:

void SomeClass::write(ofstream& originalStream){
ofstream newStream;
newStream = originalStream; // this does not work
newStream << "Hello" << endl;
}

How can I make that right? Thanks for your answers Phil


You can't copy a stream. The Standard achieves this by making the copy
constructor private.

-JKop
 
G

Gernot Frisch

Philipp said:
Hello,
How can you copy a stream (or change a reference to it)?

Imagine a function which writes "Hello" to a filestream:

void SomeClass::write(ofstream& originalStream){
ofstream newStream;
newStream = originalStream; // this does not work
newStream << "Hello" << endl;
}

How can I make that right? Thanks for your answers Phil

Why do you want to do this? There's better solutions to your problem,
I think.
 
P

Philipp

Gernot said:
Why do you want to do this? There's better solutions to your problem,
I think.

Oh I just wanted to change the "name" of the stream so I need not
rewrite some code.
 
K

Karl Heinz Buchegger

Philipp said:
Oh I just wanted to change the "name" of the stream so I need not
rewrite some code.

Could you eleborate. What does 'name of the stream' mean.

Is it the stream variables name you want to change.
if yes, then you can create a reference to the original object.

void SomeClass::write(ofstream& originalStream){
ofstream& newStream( originalStream );

newStream << "Hello" << endl;
}

Now the names 'originalStream' and 'newStream' refer to the very
same stream object. But a rewrite would be better.
 
G

Gernot Frisch

Oh I just wanted to change the "name" of the stream so I need not
That was what I was expecting.
if yes, then you can create a reference to the original object.

and that was what I wanted to suggest.
 
P

Philipp

Karl said:
Could you eleborate. What does 'name of the stream' mean.

Is it the stream variables name you want to change.
if yes, then you can create a reference to the original object.

void SomeClass::write(ofstream& originalStream){
ofstream& newStream( originalStream );

newStream << "Hello" << endl;
}

Now the names 'originalStream' and 'newStream' refer to the very
same stream object. But a rewrite would be better.

Exactly what I wanted. I just didn't know you could use a ofstream as
parameter in the ofstream constructor.

Thanks for your time.
Phil
 
K

Karl Heinz Buchegger

Philipp said:
Exactly what I wanted. I just didn't know you could use a ofstream as
parameter in the ofstream constructor.

Man. You need a book.
I am not creating a second ofstream object!
I define a reference to the object!

A reference is not an object. It is just another name
for an object. The very same ofstream object is know
now with 2 different names. That's all. No constructor
is involved.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top