copy of cout object

I was playing around with cout, and I found the following situation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;


void foo1( ostream &os) {
    os << "it works" << endl;
}
void foo2( ostream os) {
    os << "it doesn't work" << endl;
}


int main(){
     foo1(cout); //it works
     foo2(cout); //it doesn't work 
                 //usr/include/c++/4.5/bits/ios_base.h|785|error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private

}


I can pass the object cout by reference, but not by copy. Why is that?
I guess the reason of the private copy constructor is to avoid having more than one copy of cout.
Is that true?
Is there any source where I can understand the structure of iostream library without getting in too much detail?

thanks in advance!!
santiagorf
As for your first questions, yes, it's to prevent you from having multiple copies of cout. Basically, if you could make copies then you could have, say, two threads both writing to the same stream at different locations in that stream. When you pass by reference, you don't get a copy and instead things that act on the reference act on the real cout you passed in.
thanks Zhuge
Topic archived. No new replies allowed.