简体   繁体   English

使用RAII与C ++流和STL容器?

[英]Using RAII with C++ streams and STL containers?

I'm trying to use RAII concepts with an STL container of ofstream objects. 我正在尝试将RAII概念与STL容器的ofstream对象一起使用。 For example: 例如:

int main(int argc, char**argv)
{
  std::deque<std::ofstream> sList;

  sList.push_back(std::ofstream()); // tried variations such as *(new ofstream())
  sList[0].open("test1.txt");
  sList[0] << "This is a test";
  sList[0].close();
}

However, no matter how I try to tweak the code and declarations, the compiler always complains. 但是,无论我如何尝试调整代码和声明,编译器总是抱怨。 Apparently the copy constructor for std::basic_ios, which lives inside of streams, is private. 显然,生活在流内部的std :: basic_ios的拷贝构造函数是私有的。 Are there any easy plian C++/STL solutions to doing this using RAII, or do I need to get some type of smart pointer involved? 使用RAII是否有任何简单的plian C ++ / STL解决方案,或者我是否需要涉及某种类型的智能指针?

Standard library containers store copies of the values, not the values themselves. 标准库容器存储值的副本,而不是值本身。 As such, you will have to use an object that can be copied (a smart pointer in this case). 因此,你不得不使用可复制的对象(在这种情况下,智能指针)。

An alternative would be boost::ptr_vector which acts as a vector of pointers precisely for this kind of situation. 另一种方法是boost::ptr_vector ,它可以作为指针的向量,精确地boost::ptr_vector这种情况。

Stream objects cannot be copied, so you can't create containers of them - you will have to use pointers of some sort. Stream对象无法复制,因此您无法创建它们的容器 - 您必须使用某种指针。

deque <ofstream *> files;
files.push_back( new ofstream );
// and later delete it, or use a smart pointer

In the interest of keeping a file from being written to in multiple places in various parts of the system, streams are noncopyable. 为了保持文件不被写入系统的各个部分中的多个位置,流是不可复制的。 A smart pointer is probably the way to go here. 一个智能指针可能是这里的方式。

You probably need to get a smart pointer of some sort involved. 您可能需要获得某种涉及的智能指针。 One of the requirements of containers (at least in C++) is that to put something in a container, it must be copyable -- and streams are not copyable. 容器的一个要求(至少在C ++中)是将容器放入容器中,它必须是可复制的 - 并且流不可复制。

FWIW, in C++0x, this will be possible directly -- it allows containers to hold items that are movable but not copyable, and streams will be movable (but still not copyable). FWIW,在C ++ 0x中,这可以直接实现 - 它允许容器保存可移动但不可复制的项目,并且流将是可移动的(但仍然不可复制)。

ofstream has RAII baked in. The destructor of ofstream automagically closes the files so you don't need to. ofstream有RAII烘焙.ofstream的析构函数自动关闭文件,所以你不需要。

Use 采用

std::vector<boost::shared_ptr<std::ofstream>> 

as your container and all the file handles will be deleted when you drop the vector. 作为您的容器,删除向量时将删除所有文件句柄。

Do not use a container of std::auto_ptr! 不要使用std :: auto_ptr的容器!

Try using boost::ref. 尝试使用boost :: ref。 It's meant to store references without copying them. 这意味着存储引用而不复制它们。 http://www.boost.org/doc/libs/1_43_0/doc/html/ref.htm http://www.boost.org/doc/libs/1_43_0/doc/html/ref.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM