共享 PTR - C++:std::shared_ptr<T> 和 std::shared_ptr<T const> 有什么区别?
shared ptr - C++: What is the difference between std::shared_ptr<T> and std::shared_ptr<T const>?
std::shared_ptr<T>
和std::shared_ptr<T const>
之间有什么区别?
你什么时候会使用其中一种与另一种?
-
shared_ptr<int>
是一个shared_ptr
到一个非常数int
。您可以修改int和shared_ptr
-
shared_ptr<const int>
是shared_ptr
到const int
。不能修改shared_ptr
指向的const int
,因为它是const
。但是您可以修改shared_ptr
本身(分配给它,调用其他非常量方法,等等) -
const shared_ptr<int>
是const shared_ptr
到非常量int
。你不能修改shared_ptr
(通过调用reset
或任何非常量方法),但你可以修改它指向的int
-
const shared_ptr<const int>
是const shared_ptr
到const int
。你不能修改jack。
shared_ptr<T>
在引擎盖下存储一个T*,而
shared_ptr<T const>
在引擎盖下存储一个T常量*。因此,它与指向某些数据的指针和指向某些常量数据的指针之间的差异相同。
当你只想让shared_ptr表现得像一个普通指针(带有引用计数)时,你会使用它;当你想存储一个引用计数的指针到一些常量数据(你从来没有想要修改的数据)时,也会使用shared_ptr。)回想一下,指向常量数据的指针(不能修改所指向的数据,但可以修改指针)和常量指针(可以修改所指向数据,但不能修改指针本身)之间有区别。
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- EASTL矢量<向量<int>>连续的
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 什么是模板&lt;&gt;inline bla bla
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是std :: set&lt; std :: future&gt;不可能存在
- 是numeric_limits&lt; int&gt; :: is_modulo从逻辑上矛盾
- opencv 2.4.7在iOS错误背景_segm.hpp #include&lt; list&gt;未找到
- 在修改列表后,std :: list&lt; t&gt; :: end()的值是否会更改
- ///<评论></评论>在Visual Studio中