【问题标题】:Assign derived class unique_ptr to base class unique_ptr将派生类 unique_ptr 分配给基类 unique_ptr
【发布时间】:2016-06-16 05:37:29
【问题描述】:

我创建了一个从 std::istream 派生的自定义 istream,当文件是压缩文件时使用自定义 streambuf,否则使用 std::filebuf。

#mystream.h
class my_stream: public istream {
  public:
     explicit my_stream(const std::string &path);
  private:       
     std::unique_ptr<std::streambuf> b_;
}

#mystream.cpp
my_stream::my_stream(const std::string &path) :std::istream(nullptr) {
    if(path.substr(path.length()-6, path.length())==".gzip"){
        b_ = std::make_unique<gzipbuf>(path); //gzipbuf is derived from std::streambuf
    } 
    else {
        std::unique_ptr<std::filebuf> fb;
        fb->open(path.c_str(), std::ios::in);
        b_ = fb;
    }
    this->init(b_.get());
}

我可以在一个地方将派生类 unique_ptr 分配给基类 unique_ptr

b_ = std::make_unique<gzipbuf>(path);

但不是在另一个

b_ = fb;

它说

candidate function not viable: no known conversion from 'unique_ptr<std::filebuf, default_delete<std::basic_filebuf<char>>>' to 'unique_ptr<std::basic_streambuf<char>, default_delete<std::basic_streambuf<char>>>' for 1st argument
      operator=(unique_ptr&& __u) noexcept

【问题讨论】:

  • 你需要移动它:b = std::move(fb); 因为这里只能是一个(没有副本)。

标签: c++ oop c++11 unique-ptr


【解决方案1】:

首先,在这一行之后

std::unique_ptr<std::filebuf> fb;

fb 实际上并没有指向任何东西,它只是一个空的 unique_ptr 所以你在这里调用未定义的行为:

fb->open(path.c_str(), std::ios::in);

要解决这个问题,只需将行更改为:

auto fb = std::make_unique<std::filebuf>();

关于您遇到的错误,如果允许此行

b_ = fb;

然后b_ 和fb 将指向同一个对象。 unique_ptr 不允许这样做。一个资源只能由一个unique_ptr 拥有。一种解决方案是使用std::move 将所有权从fb 传递给b_:

b_ = std::move(fb)

然后fb 不再拥有任何东西。

就个人而言,我喜欢尽可能在构造函数初始化器列表中初始化成员变量,并将streambuf的创建提取到一个单独的函数中以便这样做:

std::unique_ptr<std::streambuf> createStream(const std::string &path) {
    if(path.substr(path.length()-5, path.length())==".gzip"){  // I think you meant 5 here!
        return std::make_unique<gzipbuf>(path); 
    }
    auto fb = std::make_unique<std::filebuf>();
    fb->open(path.c_str(), std::ios::in);
    return fb;
}

那么my_stream的构造函数可以是:

my_stream::my_stream(const std::string &path) : std::istream(nullptr),
  b_(createStream(path)) {
    this->init(b_.get());
}

【讨论】:

  • 谢谢!这是有道理的。
猜你喜欢
  • 2021-02-18
  • 1970-01-01
  • 2022-01-24
  • 2017-03-23
  • 2023-01-26
  • 2021-11-26
  • 2011-04-14
  • 2013-07-02
  • 1970-01-01
相关资源
最近更新 更多