what's the meaning of "stream"?

B

bo ye

For example it's really puzzled me when I wrote:

cout<<"Hi"<<flush;

and

cout<<"Hi"<<endl;

The <C++ Primer > says : the "flush " flushes the buffer , adds no
data and the "endl" inserts a newline , the flushes the buffer.

I can not understand . Please tell me how the C++ stream implements.
 
V

Victor Bazarov

For example it's really puzzled me when I wrote:

cout<<"Hi"<<flush;

and

cout<<"Hi"<<endl;

The<C++ Primer> says : the "flush " flushes the buffer , adds no
data and the "endl" inserts a newline , the flushes the buffer.

I can not understand . Please tell me how the C++ stream implements.

I think that the expression

cout<<"Hi"<<endl

is equivalent to

cout<<"Hi"<<'\n'<<flush

V
 
S

Stuart Redmann

For example it's really puzzled me when I wrote:

cout<<"Hi"<<flush;

and

cout<<"Hi"<<endl;

The <C++ Primer > says : the "flush " flushes the buffer , adds no
data and the "endl" inserts a newline , the flushes the buffer.

I can not understand . Please tell me how the C++ stream implements.

Both "endl" and "flush" are simply the names of two functions. If you
use the name of the function as you have done in your example, the
compiler actually takes the address of the function and passes it into
the stream:
For example std::endl is defined as
ostream& __cdecl endl(ostream& _outs)
{
return _outs << '\n' << flush;
}
under VC6.0 (this confirms Victors posting ;-)

You can put any function that uses the same signature as std::endl
into a stream, there is no magic involved. For example

ostream& __cdecl say_hello (ostream& _outs)
{
return _outs << "Hello World!";
}

can be passed into the stream like this:
std::cout << say_hello << std::endl;

Regards,
Stuart
 
J

Jorgen Grahn

For example it's really puzzled me when I wrote:

cout<<"Hi"<<flush;

and

cout<<"Hi"<<endl;

The <C++ Primer > says : the "flush " flushes the buffer , adds no
data and the "endl" inserts a newline , the flushes the buffer.

I can not understand . Please tell me how the C++ stream implements.

I don't quite understand your question (questions?) but an ostream
works approximately like this:

you A B fixed- C file, string
write ---> ostream ---> size ---> or other
here buffer end destination

For efficiency reasons you want to limit the number of writes at (C).
That's why the ostream normally writes into the buffer, and only
writes at (C) when the buffer is full, when you ask it to flush, and
maybe at certain other times, like when you write a '\n'.

/Jorgen
 

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

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top