为什么不允许复制stringstream? [英] Why copying stringstream is not allowed?

查看:146
本文介绍了为什么不允许复制stringstream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  int main()
{
std :: stringstream s1(This is my string。
std :: stringstream s2 = s1; //错误,不允许复制
}

为什么我不能复制stringstream。你可以提供一些参考吗?

解决方案

复制C ++中的任何流都是通过复制构造函数 private



任何表示任何,无论是 stringstream $ c> istream , ostream iostream
$ b

复制被禁用,因为它没有意义。非常非常重要的是要了解意味着什么,才能真正了解为什么复制是没有意义的。 stream 不是可以复制的容器。它不包含数据。



如果列表/向量/映射或任何容器是桶,则流是数据流的软管。将流作为一些管道通过来获取数据;管道 - 一侧是源(发送者),另一侧是宿(接收者)。这就是所谓的单向流。还存在两个方向上数据流动的双向流。那么,制作这样的东西的副本有什么意义呢?它根本不包含任何数据。



现在假设有一段时间,如果允许创建流的副本,并且您创建了一个 > std :: cin 说复制的对象是 copy_cin 。现在问自己:从 std :: cin中读取完全相同的数据时,从 copy_cin 流中读取数据是否有意义。 code>不,这没有意义,因为用户只输入一次数据,键盘(或输入设备)只生成电信号一次,它们只流过所有其他硬件和低级API一旦。

因此,不允许创建复制 em>参考

  std :: istream copy_cin = std :: cin; // error 
std :: istream& ref_cin = std :: cin; // ok

另请注意,您可以创建另一个流实例, em>底层缓冲区(旧流正在使用)。请参阅: https://ideone.com/rijov


int main()
{
   std::stringstream s1("This is my string.");
   std::stringstream s2 = s1; // error, copying not allowed
}

I couldn't find a reason as to why i can't copy stringstream. could you provide some reference?

解决方案

Copying of ANY stream in C++ is disabled by having made the copy constructor private.

Any means ANY, whether it is stringstream, istream, ostream,iostream or whatever.

Copying of stream is disabled because it doesn't make sense. Its very very very important to understand what stream means, to actually understand why copying stream does not make sense. stream is not a container that you can make copy of. It doesn't contain data.

If a list/vector/map or any container is a bucket, then stream is a hose through which data flows. Think of stream as some pipe through which you get data; a pipe - at one side is the source (sender), on the other side is the sink (receiver). That is called unidirectional stream. There're also bidirectional streams through which data flows in both direction. So what does it make sense making a copy of such a thing? It doesn't contain any data at all. It is through which you get data.

Now suppose for a while if making a copy of stream is allowed, and you created a copy of std::cin which is in fact input stream. Say the copied object is copy_cin. Now ask yourself : does it make sense to read data from copy_cin stream when the very same data has already been read from std::cin. No, it doesn't make sense, because the user entered the data only once, the keyboard (or the input device) generated the electric signals only once and they flowed through all other hardwares and low-level APIs only once. How can your program read it twice or more?

Hence, creating copy is not allowed, but creating reference 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 stream and can make it use the same underlying buffer which the old stream is currently using. See this : https://ideone.com/rijov

这篇关于为什么不允许复制stringstream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆