动态分配 std::unique_ptr 有什么用?
Any use for dynamically allocating std::unique_ptr?
使用 new
创建 std::unique_ptr
是否有意义?在下面的代码片段中,我怀疑由 std::unique_ptr
管理的 SimpleClass
对象不会被销毁,除非我自己删除 std::unique_ptr
。我想不出它在什么情况下有用,所以我想知道是否有实际使用它的情况。
std::unique_ptr<vector_test::SimpleClass>* ptr_to_unique_ptr = new std::unique_ptr<vector_test::SimpleClass>();
ptr_to_unique_ptr->reset(new vector_test::SimpleClass(555));
delete ptr_to_unique_ptr;
Does it ever make sense to create a std::unique_ptr
using new
?
很可能不会。
在已经使用标准 dynamic memory allocation techniques 的情况下,您 would/should 回退到手动内存管理没有明确的理由。
I couldn't think of a situation where this is useful so I was wondering if there are situations where this is actually used.
我也无法想象这样的情况。
很少使用动态分配单个指针。 Closest real word 用例是一个单链表,我们在其中动态分配一个 class 实例,其中包含一个指针和一些与节点关联的数据。虽然,很少需要实现链表,因为它很少是数据结构的最佳选择,而且因为标准库已经提供了不错的链表设计。
请注意,如果我们要动态分配(智能)指针,则没有充分的理由不使用智能指针来管理该分配。
Does it ever make sense to create a std::unique_ptr using new?
想不到。在某些情况下,当包含 unique_ptr
的作用域结束时,您希望阻止通过 unique_ptr
管理的对象被删除。有几种方法可以做到这一点,而不必 new
unique_ptr
本身。
一些例子:
将对象移动到不同范围内的另一个 unique_ptr
。
std::unique_ptr<int> p1;
{
std::unique_ptr<int> p2 = std::make_unique<int>( 42 );
p1 = std::move( p2 );
// Destructor of p2 won't delete object, because ownership was transferred.
}
// Scope of p2 has ended, but object is still managed by p1.
std::cout << *p1;
Return 通过函数 unique_ptr
管理的对象。
std::unique_ptr<int> MyFun() {
std::unique_ptr<int> p = std::make_unique<int>( 42 );
return p; // No explicit move required, because of RVO
}
Release 所有权并取回原始指针。
std::unique_ptr<int> p = std::make_unique<int>( 42 );
some_C_API_that_takes_ownership( p.release() );
// Destructor of p won't delete object, because ownership was given up.
使用 new
创建 std::unique_ptr
是否有意义?在下面的代码片段中,我怀疑由 std::unique_ptr
管理的 SimpleClass
对象不会被销毁,除非我自己删除 std::unique_ptr
。我想不出它在什么情况下有用,所以我想知道是否有实际使用它的情况。
std::unique_ptr<vector_test::SimpleClass>* ptr_to_unique_ptr = new std::unique_ptr<vector_test::SimpleClass>();
ptr_to_unique_ptr->reset(new vector_test::SimpleClass(555));
delete ptr_to_unique_ptr;
Does it ever make sense to create a
std::unique_ptr
usingnew
?
很可能不会。
在已经使用标准 dynamic memory allocation techniques 的情况下,您 would/should 回退到手动内存管理没有明确的理由。
I couldn't think of a situation where this is useful so I was wondering if there are situations where this is actually used.
我也无法想象这样的情况。
很少使用动态分配单个指针。 Closest real word 用例是一个单链表,我们在其中动态分配一个 class 实例,其中包含一个指针和一些与节点关联的数据。虽然,很少需要实现链表,因为它很少是数据结构的最佳选择,而且因为标准库已经提供了不错的链表设计。
请注意,如果我们要动态分配(智能)指针,则没有充分的理由不使用智能指针来管理该分配。
Does it ever make sense to create a std::unique_ptr using new?
想不到。在某些情况下,当包含 unique_ptr
的作用域结束时,您希望阻止通过 unique_ptr
管理的对象被删除。有几种方法可以做到这一点,而不必 new
unique_ptr
本身。
一些例子:
将对象移动到不同范围内的另一个
unique_ptr
。std::unique_ptr<int> p1; { std::unique_ptr<int> p2 = std::make_unique<int>( 42 ); p1 = std::move( p2 ); // Destructor of p2 won't delete object, because ownership was transferred. } // Scope of p2 has ended, but object is still managed by p1. std::cout << *p1;
Return 通过函数
unique_ptr
管理的对象。std::unique_ptr<int> MyFun() { std::unique_ptr<int> p = std::make_unique<int>( 42 ); return p; // No explicit move required, because of RVO }
Release 所有权并取回原始指针。
std::unique_ptr<int> p = std::make_unique<int>( 42 ); some_C_API_that_takes_ownership( p.release() ); // Destructor of p won't delete object, because ownership was given up.