std::shared_ptr::shared_ptr
来自cppreference.com
< cpp | memory | shared ptr
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
constexpr shared_ptr(); |
(1) | |
template< class Y > explicit shared_ptr( Y* ptr ); |
(2) | |
template< class Y, class Deleter > shared_ptr( Y* ptr, Deleter d ); |
(3) | |
template< class Y, class Deleter, class Alloc > shared_ptr( Y* ptr, Deleter d, Alloc alloc ); |
(4) | |
constexpr shared_ptr( std::nullptr_t ); |
(5) | |
template< class Deleter > shared_ptr( std::nullptr_t, Deleter d ); |
(6) | |
template< class Deleter, class Alloc > shared_ptr( std::nullptr_t, Deleter d, Alloc alloc ); |
(7) | |
template< class Y > shared_ptr( const shared_ptr<Y>& r, T *ptr ); |
(8) | |
shared_ptr( const shared_ptr& r ); |
(9) | |
template< class Y > shared_ptr( const shared_ptr<Y>& r ); |
(9) | |
shared_ptr( shared_ptr&& r ); |
(10) | |
template< class Y > shared_ptr( shared_ptr<Y>&& r ); |
(10) | |
template< class Y > explicit shared_ptr( const std::weak_ptr<Y>& r ); |
(11) | |
template< class Y > shared_ptr( std::auto_ptr<Y>&& r ); |
(12) | |
template< class Y, class Deleter > shared_ptr( std::unique_ptr<Y,Deleter>&& r ); |
(13) | |
构建新的
shared_ptr
从各种指针类型,是指一个对象来管理。可以提供可选的删除d
,这是用来摧毁的对象时,没有shared_ptr
对象拥有它。默认情况下,作为删除的表情表达的删除。总是选择正确的delete表达提供的类型对应,这是使用一个单独的参数Y
为模板的构造函数来实现的原因.原文:
Constructs new
shared_ptr
from a variety of pointer types that refer to an object to manage. Optional deleter d
can be supplied, which is later used to destroy the object when no shared_ptr
objects own it. By default, 删除的表情 expression is used as deleter. Proper delete expression corresponding to the supplied type is always selected, this is the reason why the constructors are implemented as templates using a separate parameter Y
.如果指向的对象
ptr
已经拥有一些共享的指针,该构造函数会导致未定义行为.原文:
If the object pointed to by
ptr
is already owned by some shared pointer, the constructor results in undefined behavior.1)
默认构造函数构造一个没有管理的对象,即空
shared_ptr
shared_ptr
原文:
Default constructor constructs a
shared_ptr
with no managed object, i.e. empty shared_ptr
2-4)
构造一个
shared_ptr
ptr
被管理的对象。 Y
必须是一个完整的类型和隐式转换为T
。此外原文:
Constructs a
shared_ptr
with ptr
as the managed object. Y
must be a complete type and implicitly convertible to T
. Additionally:2)
使用delete表达式的Deleter的。必须提供有效的删除表达式,即delete ptr必须有充分的形成,有定义良好的行为并没有抛出任何异常.
原文:
Uses the delete expression as the deleter. A valid delete expression must be available, i.e. delete ptr must be well formed, have well-defined behavior and not throw any exceptions.
3)
使用指定的删除
d
的Deleter的。 Deleter
的类型必须是可调用的T
,即d(ptr)必须有充分的形成,具有定义良好的行为并没有抛出任何异常。 Deleter
必须CopyConstructible
.原文:
Uses the specified deleter
d
as the deleter. Deleter
must be callable for the type T
, i.e. d(ptr) must be well formed, have well-defined behavior and not throw any exceptions. Deleter
must be CopyConstructible
.4)
5-7)
类似(2),(3),(4),但没有管理的对象构造一个
shared_ptr
,即空shared_ptr
原文:
Analogous to (2), (3), (4) respectively, but constructs a
shared_ptr
with no managed object, i.e. empty shared_ptr
.8)
构造一个
shared_ptr
共享所有权信息r
,但拥有一个不相关的和非托管的指针ptr
。即使shared_ptr
是该组的最后一个走出去的范围,它会调用析构函数的对象,最初管理的r
。然而,在此调用get()
将总是返回一个副本ptr
。这是程序员的责任,以确保ptr
,这仍然是有效的,只要存在这个shared_ptr,如在典型的用例ptr
是一个r
管理的对象,或者是一个别名(例如,垂头丧气) r.get()
原文:
Constructs a
shared_ptr
which shares ownership information with r
, but holds an unrelated and unmanaged pointer ptr
. Even if this shared_ptr
is the last of the group to go out of scope, it will call the destructor for the object originally managed by r
. However, calling get()
on this will always return a copy of ptr
. It is the responsibility of the programmer to make sure that this ptr
remains valid as long as this shared_ptr exists, such as in the typical use cases where ptr
is a member of the object managed by r
or is an alias (e.g., downcast) of r.get()
9)
构造一个
shared_ptr
这股r
管理的对象的所有权。如果r
管理对象,*this
管理也没有对象。模板超载不参与重载决议Y*
是不隐式转换为T*
的原文:
Constructs a
shared_ptr
which shares ownership of the object managed by r
. If r
manages no object, *this
manages no object too. The templated overload doesn't participate in the overload resolution if Y*
is not implicitly convertible to T*
.10)
从
shared_ptr
移动构建了一个r
。施工结束后,*this包含以前的状态r
的副本,r
是空的。模板超载不参与重载决议Y*
是不隐式转换为T*
的原文:
Move-constructs a
shared_ptr
from r
. After the construction, *this contains a copy of the previous state of r
, r
is empty. The templated overload doesn't participate in the overload resolution if Y*
is not implicitly convertible to T*
.11)
构造一个
shared_ptr
这股r
管理的对象的所有权。 Y*
必须转换为T*
。需要注意的是r.lock()可用于同样的目的:不同的是,该构造函数抛出一个异常,如果参数为空,而在这种情况下,std::weak_ptr<T>::lock()构造一个空的std::shared_ptr
.原文:
Constructs a
shared_ptr
which shares ownership of the object managed by r
. Y*
must be convertible to T*
. Note that r.lock() may be used for the same purpose: the difference is that this constructor throws an exception if the argument is empty, while std::weak_ptr<T>::lock() constructs an empty std::shared_ptr
in that case.12)
构造一个
shared_ptr
存储,并拥有以前所拥有的r
的对象。 Y*
必须转换为T*
。项目建成后,r
是空原文:
Constructs a
shared_ptr
that stores and owns the object formerly owned by r
. Y*
must be convertible to T*
. After construction, r
is empty.13)
构造一个
shared_ptr
目前由r
管理对象。存储未来的管理对象删除的删除相关的r
。 r
管理后没有对象的调用。 Y*
必须转换为T*
.原文:
Constructs a
shared_ptr
which manages the object currently managed by r
. The deleter associated to r
is stored for future deletion of the managed object. r
manages no object after the call. Y*
must be convertible to T*
.目录 |
[编辑] 注释
建设,从原料的指针来自std::enable_shared_from_this的类型的对象时,构造函数的
shared_ptr
发现其他业主的对象通过弱引用存储在该基类和股权,而不是假设不被管理的对象.原文:
When constructing from a raw pointer to an object of a type derived from std::enable_shared_from_this, the constructors of
shared_ptr
detect other owners of the object through the weak reference stored in that base class and share ownership with them, instead of assuming that the object is not managed.[编辑] 参数
ptr | - | 一个指针,指向一个对象管理
|
d | - | 使用一个的删除销毁对象
原文: a deleter to use to destroy the object |
alloc | - | 一个分配器,用于分配内部使用的数据
原文: an allocator to use for allocations of data for internal use |
r | - | 另一个智能指针共享所有权或收购的所有权
原文: another smart pointer to share the ownership to or acquire the ownership from |
[编辑] 例外
1)
2)
std::bad_alloc如果无法获得所需的额外内存。可能会引发其他错误的实现定义的异常。 delete ptr如果出现异常,被称为.
原文:
std::bad_alloc if required additional memory could not be obtained. May throw implementation-defined exception for other errors. delete ptr is called if an exception occurs.
3-4)
std::bad_alloc如果无法获得所需的额外内存。可能会引发其他错误的实现定义的异常。 d(ptr)如果出现异常,被称为.
原文:
std::bad_alloc if required additional memory could not be obtained. May throw implementation-defined exception for other errors. d(ptr) is called if an exception occurs.
5)
6-7)
std::bad_alloc如果无法获得所需的额外内存。可能会引发其他错误的实现定义的异常。如果抛出一个异常,d(ptr)执行.
原文:
std::bad_alloc if required additional memory could not be obtained. May throw implementation-defined exception for other errors. If an exception is thrown, d(ptr) is executed.
8-10)
11)
std::bad_weak_ptrr.expired == true。在这种情况下,构造函数有没有影响.....
原文:
std::bad_weak_ptr if r.expired == true. The constructor has no effect in this case.
12)
std::bad_alloc如果无法获得所需的额外内存。可能会引发其他错误的实现定义的异常。这个构造函数有没有效果,如果出现异常,.
原文:
std::bad_alloc if required additional memory could not be obtained. May throw implementation-defined exception for other errors. This constructor has no effect if an exception occurs.
13)
如果一个异常被抛出时,构造函数没有影响.
原文:
If an exception is thrown, the constructor has no effects.
[编辑] 示例
#include <memory> #include <iostream> struct Foo { Foo() { std::cout << "Foo...\n"; } ~Foo() { std::cout << "~Foo...\n"; } }; struct D { void operator()(Foo* p) const { std::cout << "Call delete for Foo object...\n"; delete p; } }; int main() { // constructor with no managed object std::shared_ptr<Foo> sh1; // constructor with object std::shared_ptr<Foo> sh2(new Foo); std::shared_ptr<Foo> sh3(sh2); std::cout << sh2.use_count() << '\n'; std::cout << sh3.use_count() << '\n'; //constructor with object and deleter std::shared_ptr<Foo> sh4(new Foo, D()); }
输出:
Foo... 2 2 Foo... Call delete for Foo object... ~Foo... ~Foo...
[编辑] 另请参阅
创建一个共享管理一个新的对象的指针,该指针 原文: creates a shared pointer that manages a new object (函数模板) | |
创建一个共享指针管理使用分配器分配一个新的对象 原文: creates a shared pointer that manages a new object allocated using an allocator (函数模板) |