C++ 从原始指针创建 shared_ptr

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4665266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:05:31  来源:igfitidea点击:

Creating shared_ptr from raw pointer

c++

提问by Leslieg

I have a pointer to an object. I would like to store it in two containers which both have the ownership. So I think I would be good to make it a shared_ptr of C++0x. How could I convert a raw pointer to a shared_pointer?

我有一个指向对象的指针。我想将它存储在两个都拥有所有权的容器中。所以我想我最好让它成为 C++0x 的 shared_ptr。如何将原始指针转换为 shared_pointer?

typedef unordered_map<string, shared_ptr<classA>>MAP1;
MAP1 map1;
classA* obj = new classA();
map1[ID] = how could I store obj in map1??

Thanks

谢谢

回答by Dawson

You need to make sure you don't initialize both shared_ptr objects with the same raw pointer, or it will be deleted twice. A better (but still bad) way to do it:

您需要确保不要使用相同的原始指针初始化两个 shared_ptr 对象,否则它将被删除两次。一个更好(但仍然很糟糕)的方法:

classA* raw_ptr = new classA;
shared_ptr<classA> my_ptr(raw_ptr);

// or shared_ptr<classA> my_ptr = raw_ptr;

// ...

shared_ptr<classA> other_ptr(my_ptr);
// or shared_ptr<classA> other_ptr = my_ptr;
// WRONG: shared_ptr<classA> other_ptr(raw_ptr);
// ALSO WRONG: shared_ptr<classA> other_ptr = raw_ptr;

WARNING: the above code shows bad practice! raw_ptrsimply should not exist as a variable. If you directly initialize your smart pointers with the result of new, you reduce your risk of accidentally initializing other smart pointers incorrectly. What you should do is:

警告:上面的代码显示了不好的做法!raw_ptr根本不应该作为变量存在。如果您使用 的结果直接初始化您的智能指针new,您可以降低意外错误地初始化其他智能指针的风险。你应该做的是:

shared_ptr<classA> my_ptr(new classA);

shared_ptr<classA> other_ptr(my_ptr);

What's nice is that the code is more concise as well.

好的是代码也更简洁。

EDIT

编辑

I should probably elaborate on how it would work with a map. If you had a raw pointer and two maps, you could do something similar to what I showed above.

我可能应该详细说明它如何与地图一起工作。如果你有一个原始指针和两个映射,你可以做一些类似于我上面展示的事情。

unordered_map<string, shared_ptr<classA> > my_map;
unordered_map<string, shared_ptr<classA> > that_guys_map;

shared_ptr<classA> my_ptr(new classA);

my_map.insert(make_pair("oi", my_ptr));
that_guys_map.insert(make_pair("oi", my_ptr));
// or my_map["oi"].reset(my_ptr);
// or my_map["oi"] = my_ptr;
// so many choices!

回答by Keith

You can use a variety of ways, but reset() would be good:

您可以使用多种方法,但 reset() 会很好:

map1[ID].reset(obj);

And to address the issue of having two maps refer to the same shared_ptr, we can have:

为了解决两个映射引用同一个 shared_ptr 的问题,我们可以有:

map2[ID] = map1[ID];

Note that the trick in general to avoid a double delete is to try to avoid raw pointers at all. Hence avoid:

请注意,避免双重删除的一般技巧是尽量避免使用原始指针。因此避免:

classA* obj = new classA();
map1[ID].reset(obj);

but instead put the new heap object straight into a shared_ptr.

而是将新的堆对象直接放入 shared_ptr 中。