Copying ANY stream in C ++ is disabled by making the copy constructor private .
Any means ANY, whether it is stringstream , istream , ostream , iostream or something else.
Copying a stream is disabled because it does not make sense. It is very important to understand what a stream means in order to understand why copying a stream makes no sense. stream not a container that you can make. It does not contain data.
If the list / vector / map or any container is a bucket, then the stream is the hoses through which the data flows. Think of a stream as some kind of pipe through which you receive data; pipe - on the one hand - the source (sender), on the other hand - the receiver (receiver). This is called unidirectional flow. There are also bidirectional streams through which data flows in both directions. So what does it mean to make a copy of this? It does not contain any data. It is through this that you receive the data.
Suppose for a while, if you make a copy of the stream is valid, and you created a copy of std::cin , which is actually the input stream. Say the copied copy_cin object. Now ask yourself: it makes sense to read data from the copy_cin stream when the same data has already been read from std::cin. No, this does not make sense, because the user entered the data only once, the keyboard (or input device) generated electrical signals only once, and they flowed through all other hardware and the low-level API only once. How can your program read it twice or more?
Therefore, creating a copy is not allowed, but linking is allowed:
std::istream copy_cin = std::cin; //error std::istream & ref_cin = std::cin; //ok
Also note that you can create another instance of the stream and make it the same base buffer that the old stream uses. See this: https://ideone.com/rijov
Nawaz May 15 '11 at 20:10 2011-05-15 20:10
source share