cin and cout as arguments of a function.

Is it possible to accept objects like cin,cout and the objects of ifstream fstream(file buffers) as arguments and then doing operations on them? If yes, please tell how.
Yes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>

// these should normally be included automatically
// #include <istream>
// #include <ostream>

void writeString(std::ostream &os, const std::string s)
{
	os << s << std::endl;
}

int main()
{
	writeString(std::cout, "Hello, World!");
}
I don't know if its stupid to ask this but is it necessary to call it by Reference?
Have you tried calling without a reference?
I can't test this myself right now, but something tells me std::cout and family aren't copyable. Besides that, operator<< changes the stream, and I don't know what may happen if those changes were allowed to be "lost".
When I try calling it by value i get this error

error: `std::ios_base::ios_base(const std::ios_base&)' is private


And could you tell one more related thing. When I do this :
cout<<(cout); I get this
0x44334c4
which is most probably a pointer(although cout is an object). Why is that ?
Stream objects cannot be copied or assigned, for good reasons. Here are some words:

http://stdcxx.apache.org/doc/stdlibug/34-2.html


Why is that ?

It's the address of the object.
Topic archived. No new replies allowed.