结构字段上的智能指针

smart pointer on field of struct

本文关键字:智能 指针 字段 结构      更新时间:2023-10-16

我读到不能将std::auto_ptr存储在std::vector中,可以使用boost::ptr_vector代替。我已经能够这样做,但是我不知道如何使用ptr_vector,当我不想存储指针,而是一个具有指针成员

的结构时。

在此示例中,我想打开一些文件并将关联的 ofstream 对象与一些其他数据存储在一起,供以后使用。我想用智能指针替换struct datafile字段。由于vector<data> v应该是所有者,我认为shared_ptr可以工作,但不合适。

我应该用什么替换裸指针file

#include <iostream>
#include <fstream>
#include <vector>
struct data {
  std::string filename;
  std::ofstream* file;
  data(const std::string filename, std::ofstream* file)
    : filename(filename), file(file)
  {
  }
};
std::vector<data> open_files()
{
  std::vector<data> v;
  v.push_back(data("foo", new std::ofstream("foo")));
  return v;
}
int main()
{
  std::vector<data> v = open_files();
  /* use the files */
  *(v[0].file) << "foo";
  delete v[0].file;  // either rely on dtor to close(), or call it manually
}

更新:我觉得我在描述我的问题方面做得不够理想,让我尝试另一个例子。我也在寻找 C++03 解决方案:

#include <memory>
#include <vector>
#include <boost/ptr_container/ptr_vector.hpp>
struct T {
  std::auto_ptr<int> a;
};
int main()
{
  // instead of
  // std::vector<std::auto_ptr<int> > v;
  // use
  boost::ptr_vector<int> v;
  // what to use instead of
  // std::vector<T> w;
}

关于您的数据类,我建议使用 std::unique_ptr<std::ofstream> .这不是为了避免意外的内存泄漏,因为您要删除构造函数中的指针,而是使所有权显式。代码的用户必须知道data在构造函数中使用它所采用的指针做什么:

std::ofstream ofs;
{
   data d1("crash", &ofs);
} // error! d1 will attempt to delete stack allocated object
std::ofstream* pOfs = new std::ofstream(....);
data d2("crash again", pOfs);
delete pOFs; // user thinks data makes a deep copy

但是,unique_ptr意图是明确的,因此更难犯错误:

data d3("OK", std::unique_ptr<std::ofstream>(new std::ofstream(....)));
std::unique_ptr<std::ofstream> pOfs2(new std::ofstream(....));
data d4("OK", pOfs2); // safe, pOfs's contents have been safely moved
// we can check pOfs2 after the move
if (pOfs2) { /*  */ } 

您可以在析构函数中删除指针:

struct data 
{
  std::string filename;
  std::ofstream* file;
  data(const std::string filename, std::ofstream* file)
    : filename(filename), file(file)
  {
  }
  ~data()
  {
     delete file;
  }
};

或者使用std::unique_ptr来包装该指针,但在您的情况下是不必要的。

您不需要将 ofstream* 作为成员。

#include <iostream>
#include <fstream>
#include <vector>
struct data {
  std::string filename;
  data(const std::string filename) : filename(filename)
  {}
};
std::vector<data> open_files()
{
  std::vector<data> v;
  v.push_back(data("foo"));
  return v;
}

如果要追加到文件,请指定应用文件模式。

void print_files(const std::vector<data>& v)
{
    for(std::vector<data>::const_iterator it = v.begin(); it != v.end(); ++it)
    {
        std::ofstream os(it->filename, std::ostream::app);
        os << "bar";
    }
}
int main()
{
  std::vector<data> v = open_files();
    print_files(v);
}