简体   繁体   English

如何将push_back unique_ptr参数推到共享ptrs的向量上

[英]How push_back unique_ptr parameter onto vector of shared ptrs

I'm having a tough time pushing back a unique_ptr from my method parameter onto a vector of shared pointers. 我很难过将我的方法参数中的unique_ptr推回共享指针的向量中。

IFCCB.h: IFCCB.h:

private:
vector<shared_ptr<IFC>>  m_shpVectorIFC;
public:
void addElementVectorIFC(unique_ptr<IFC> rupIFC);

IFCCB.cpp: IFCCB.cpp:

void IFCCB::addElementVectorIFC(unique_ptr<IFC> rupIFC)
{
    m_shpVectorIFC.push_back(std::unique_ptr<IFC>(new IFContent(rupIFC)));
}

I'm getting the error: 我收到错误消息:

C2664: 'IFC::IFC(const IFC &)' : cannot convert argument 1 from 'std::unique_ptr>' to 'IFO *' C2664:'IFC :: IFC(const IFC&)':无法将参数1从'std :: unique_ptr>'转换为'IFO *'

In this case, IFO is the heirarchical parent of IFC. 在这种情况下,IFO是IFC的继承父级。 I'm not sure why it's looking at that. 我不确定为什么要这么做。

I've looked at vector info and shared_ptr info , as well as using unique_ptr with standard library containers . 我研究了矢量信息shared_ptr信息 ,以及将unique_ptr与标准库容器一起使用

Any ideas? 有任何想法吗? I'm not used to working with shared_ptrs and unique_ptrs. 我不习惯使用shared_ptrs和unique_ptrs。

The problem is that push_back takes the container's value_type , which is shared_ptr<IFC> , but you are passing it a unique_ptr<IFC> and the conversion from unique_ptr to shared_ptr uses an explicit constructor and can only be done from a unique_ptr rvalue, so the argument cannot be implicitly converted to shared_ptr . 问题是push_back接受了容器的value_type ,即shared_ptr<IFC> ,但是您将其传递给unique_ptr<IFC>并且从unique_ptrshared_ptr的转换使用explicit构造函数,并且只能从unique_ptr右值完成,因此参数不能隐式转换为shared_ptr

To make it work you need to use std::move to convert the unique_ptr to an rvalue and then either do the conversion to shared_ptr explicitly: 要使其工作,您需要使用std::moveunique_ptr转换为一个右值,然后明确地将其转换为shared_ptr

unique_ptr<IFC> p;
// ...
m_shpVectorIFC.push_back(std::shared_ptr<IFC>(std::move(p)));

Or use emplace_back instead, because that function can use explicit constructors to construct the new container element: 或者使用emplace_back代替,因为该函数可以使用explicit构造函数来构造新的容器元素:

m_shpVectorIFC.emplace_back(std::move(p)));

I'm not convinced your code that creates a new unique_ptr is correct (why can't you just insert rupIFC into the container using either of the solutions shown above?) but if that's really what you want to do, the error you get is because you are trying to pass unique_ptr<IFC> to the IFContent constructor, which takes a IFO* not a unique_ptr<IFC> . 我不认为您创建新的unique_ptr代码是正确的(为什么不能使用上面显示的任何一种解决方案将rupIFC插入容器中?)但是,如果这确实是您想要做的,那么您得到的错误是因为您尝试将unique_ptr<IFC>传递给IFContent构造函数,该构造函数采用IFO*而不是unique_ptr<IFC> To make that compile you need to get the raw pointer out of rupIFC : 为了进行编译,您需要从rupIFC获取原始指针:

std::unique_ptr<IFC>(new IFContent(rupIFC.get()))

However this is probably unsafe, because the pointer you passed to the IFContent constructor will be deleted at the end of the function when rupIFC is destroyed, so maybe you meant to release it: 但是,这可能是不安全的,因为当rupIFC被销毁时,传递给IFContent构造函数的指针将在函数末尾删除,因此也许您打算释放它:

std::unique_ptr<IFC>(new IFContent(rupIFC.release()))

NB as dlf's answer says, there is no point creating a unique_ptr if you just want to convert it to a shared_ptr immediately, so you could simply do: 注意,如dlf的回答所述,如果只想立即将其转换为shared_ptr ,则没有必要创建unique_ptr ,因此您可以简单地执行以下操作:

m_shpVectorIFC.emplace_back(std::make_shared<IFContent>(rupIFC.release()));

Based on your addendum, you will need to use unique_ptr::get() to provide the IFContent constructor with the raw pointer it wants. 根据您的附录,您将需要使用unique_ptr::get()IFContent构造函数提供所需的原始指针。 Depending on what it does with that pointer, you may actually need to use release instead to prevent double-deletion. 根据该指针的作用,您实际上可能需要使用release来防止双重删除。 Also, no need to create an intermediate unique_ptr when it's just going to be converted right into a shared_ptr anyway: 另外,无论如何,只要将其直接转换为shared_ptr ,就无需创建中间的unique_ptr

void IFCCB::addElementVectorIFC(unique_ptr<IFC> rupIFC)
{
    m_shpVectorIFC.push_back(std::shared_ptr<IFC>(new IFContent(rupIFC.get())));
}

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

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