Dan*_*y A -7 c++ make-shared c++11
当std::make_shared(new Foo())调用它时,它构造一个Foo并std::shared_ptr<Foo>为调用者返回一个(即这里).如果从各种对象多次调用它,它new Foo()每次构造一次吗?在这种情况下,它是否与每个调用者获得对新对象的单个引用没有什么不同,在实用性上像unqiue_ptr一样?
或者它Foo()是第一次创建一个然后随后返回std::shared_ptr它,知道它将像一个单独的类型(当然删除一旦最后一个std::shared_ptr被删除)?这个平台是否具体?
特别是这样的功能:
std::shared_ptr<Foo> makeFoo()
{
return std::make_shared<Foo>();
}
Run Code Online (Sandbox Code Playgroud)
不,std::make_shared<Foo>()将始终创建一个新的Foo对象并将托管指针返回给它.
不同之处unique_ptr在于,您可以对指针进行多次引用,而unique_ptr对您的对象只有一个生命引用.
auto x = std::make_shared<Foo>();
auto y = x; // y and x point to the same Foo
auto x1 = std::make_unique<Foo>();
auto y1 = x1; // Does not work
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
650 次 |
| 最近记录: |