简体   繁体   English

如何创建一个到std :: vector的shared_ptr?

[英]How can I create a shared_ptr to a std::vector?

I need to create a shared_ptr to a std::vector, what is the correct syntax? 我需要为std :: vector创建一个shared_ptr,正确的语法是什么?

std::vector<uint8_t> mVector;
shared_ptr<std::vector<uint8_t>> mSharedPtr = &mVector;

The code above does not compile. 上面的代码不能编译。

Thanks. 谢谢。

What you are trying to do is to let a smart pointer manage a stack object. 你要做的是让一个智能指针管理一个堆栈对象。 This doesn't work, as the stack object is going to kill itself when it goes out of scope. 这不起作用,因为堆栈对象在超出范围时会自行终止。 The smart pointer can't prevent it from doing this. 智能指针不能阻止它这样做。

std::shared_ptr<std::vector<uint8_t> > sp;
{
   std::vector<uint8_t> mVector;
   sp=std::shared_ptr<std::vector<uint8_t> >(&mVector);
}
sp->empty();   // dangling reference, as mVector is already destroyed

Three alternatives: 三种选择:

(1) Initialize the vector and let it manage by the shared_ptr : (1)初始化向量并让它由shared_ptr管理:

auto mSharedPtr = std::make_shared<std::vector<uint8_t> >(/* vector constructor arguments*/);


(2) Manage a copy of the vector (by invoking the vector copy constructor): (2)管理向量的副本(通过调用向量复制构造函数):

std::vector<uint8_t> mVector;
auto mSharedPtr = std::make_shared<std::vector<uint8_t> >(mVector);


(3) Move the vector (by invoking the vector move constructor): (3)移动向量(通过调用向量移动构造函数):

std::vector<uint8_t> mVector;
auto mSharedPtr = std::make_shared<std::vector<uint8_t> >(std::move(mVector));
//don't use mVector anymore.

First, what you're doing is very wrong, if you give a pointer to a shared_ptr make sure it's dynamically allocated with new , not on the stack. 首先,你在做什么是非常错误的,如果你给一个指向一个shared_ptr确保它与动态分配的new ,不是在栈中。 Otherwise you may just as well use a pointer instead of a shared_ptr. 否则你也可以使用指针而不是shared_ptr。

Your code should be: 你的代码应该是:

std::vector<uint8_t> mVector;
/* Copy the vector in a shared pointer */
std::shared_ptr<std::vector<uint8_t> > mSharedPtr ( new std::vector<uint8_t>(mVector) );

or: 要么:

std::shared_ptr<std::vector<uint8_t> > mSharedPtr ( new std::vector<uint8_t>() );

As for why it doesn't compile, you need to use the constructor, not the = operator. 至于为什么它不编译,你需要使用构造函数,而不是=运算符。

As pointed out by @remyabel, make_shared is more efficient: 正如@remyabel所指出的, make_shared更有效:

std::vector<uint8_t> vector;
/* Copy the vector in a shared pointer */
auto sharedPtr = std::make_shared<std::vector<uint8_t>> (vector);

your code doesn't compile because you are assigning a raw pointer ' &mVector ' to smart pointer ' mSharedPtr ' which are two different objects and no casting is allowed. 您的代码无法编译,因为您将原始指针' &mVector '分配给智能指针' mSharedPtr ',它是两个不同的对象,不允许进行转换。

you can do that by other approaches 你可以通过其他方法做到这一点

(1) intializing your shared_ptr with the raw pointer from the begining (1)使用来自开头的原始指针初始化您的shared_ptr

std::shared_ptr<std::vector<uint8_t>> sPtr (&mVector);

(2) using reset() method of shared_ptr (2)使用shared_ptr reset()方法

std::shared_ptr<std::vector<uint8_t>> sPtr;
sPtr.reset(&mVector);

assigning a stack object raw pointer to smart pointer , you should also supply an empty deleter to the smart pointer, so that the smart pointer doesn't delete the object when it is still on the stack. 将堆栈对象原始指针指定给智能指针,您还应该为智能指针提供一个空删除器,以便智能指针在堆栈仍然存在时不会删除该对象。

std::shared_ptr<std::vector<uint8_t>> sPtr (&mVector,[](std::vector<uint8_t>*){});

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

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