社区
C++ 语言
帖子详情
std::shared_ptr和boost_shared_ptr区别?
zhousitiaoda
2014-10-14 11:29:40
项目中用到了大量boost::shared_ptr,现在想全部替换成std::shared_ptr,应该没什么问题吧?两者实现的代码略略看了下,提供的接口大都相同,内部实现没细看。请教下替换的话有什么额外需要注意的?或者说某些接口两者的差异性很大?
...全文
805
5
打赏
收藏
std::shared_ptr和boost_shared_ptr区别?
项目中用到了大量boost::shared_ptr,现在想全部替换成std::shared_ptr,应该没什么问题吧?两者实现的代码略略看了下,提供的接口大都相同,内部实现没细看。请教下替换的话有什么额外需要注意的?或者说某些接口两者的差异性很大?
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
5 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
7zkeeper
2017-03-15
打赏
举报
回复
有些情况不行
zybjtu
2014-10-14
打赏
举报
回复
应该没问题。std也是从boost借鉴的。boost保留了自己的share_ptr. 接口基本一样
勤奋的小游侠
2014-10-14
打赏
举报
回复
应该可以。你自己写个例子测试一下呗
zhousitiaoda
2014-10-14
打赏
举报
回复
所得寺内。。。感谢各位。。
mujiok2003
2014-10-14
打赏
举报
回复
我的项目中做过转换,没有发现问题
shared
_from_this() in Constructor:直接替换
std
::
shared
_
ptr
+
std
:: enable_
shared
_from_this-开源
显然,许多人不喜欢标准
std
:: enable_
shared
_from_this类不允许在构造函数中调用
shared
_from_this()。 猜猜是什么:应该填补这个空白。
boost
库也可以这样做,但是它不允许在析构函数中创建
shared
_
ptr
s,并且它不提供release()方法来获取所包含指针的所有权。 但是,不利的一面是,它还没有成为线程安全的。
shared
_
ptr
shared
_
ptr
boost
audio video AVDataPool
C++系列第2:C++2.0(11+14+17+20)详解及多线程实战
90 5.5.1 RAII 与引用计数 90 5.5.2
std
::
shared
_
ptr
90 5.5.3
std
::unique_
ptr
91 5.5.4
std
::weak_
ptr
91 第6章 C++2.0多线程原理与实战 93 什么是并发 93 并发的方式 93 为什么使用并发 95 线程简介 96 创建线程...
浅析
Boost
智能指针:scoped_
ptr
shared
_
ptr
weak_
ptr
一. scoped_
ptr
boost
::scoped_
ptr
和
std
::auto_
ptr
非常类似,是一个简单的智能指针,它能够保证在离开作用域后对象被自动释放。下列代码演示了该指针的基本应用: 代码如下:#include
#include
#include <
boost
> class implementation{public: ~implementation() {
std
::cout <<“destroying implementation\n”; } void do_something() {
std
::cou
C++智能指针详解.pdf
C++智能指针详解 智能指针详解 智能指针内容很多,重点是基本⽤法。 #include <
boost
/
shared
_
ptr
.hpp> class CBase: public
boost
::enable_
shared
_from_this
{ public: virtual void f(){}//必须有个虚函数才能向上向下转换。 } typedef
boost
::
shared
_
ptr
CBase
Ptr
; class CChild: public CBase {} typedef
boost
::
shared
_
ptr
CChild
Ptr
; void main() { CBase
Ptr
ptr
Base =
boost
::make_
shared
(); //CBase
Ptr
ptr
Base = CBase
Ptr
(new CBase()); // 向下转换 CChild
Ptr
ptr
Child =
boost
::dynamic_pointer_cast
(
ptr
Base); // 向上转换 CBase
Ptr
ptr
XXX =
ptr
Child; // 普通转换 CChild
Ptr
ptr
XX = CChild
Ptr
(dynamic_cast
(
ptr
XXX.get())); } 暂时学会这些⽤法即可。 url: C++ 智能指针详解 ⼀、简介 由于 C++ 语⾔没有⾃动内存回收机制,程序员每次 new 出来的内存都要⼿动 delete。程序员忘记 delete,流程太复杂,最终导致没 有 delete,异常导致程序过早退出,没有执⾏ delete 的情况并不罕见。 ⽤智能指针便可以有效缓解这类问题,本⽂主要讲解参见的智能指针的⽤法。包 括:
std
::auto_
ptr
boost
::scoped_
ptr
、
boost
::
shared
_
ptr
、
boost
::scoped_array、、
boost
::weak_
ptr
、
boost
::intrusive_
ptr
。你可能会想,如 此多的智能指针就为了解决new、delete匹配问题,真的有必要吗?看完这篇⽂章后,我想你⼼⾥⾃然会有答案。 下⾯就按照顺序讲解如上 7 种智能指针(smart_
ptr
)。 ⼆、具体使⽤ 1、总括 对于编译器来说,智能指针实际上是⼀个栈对象,并⾮指针类型,在栈对象⽣命期即将结束时,智能指针通过析构函数释放有它管理的 堆内存。所有智能指针都重载了"operator->"操作符,直接返回对象的引⽤,⽤以操作对象。访问智能指针原来的⽅法则使⽤"."操作符。 访问智能指针包含的裸指针则可以⽤ get() 函数。由于智能指针是⼀个对象,所以if (my_smart_object)永远为真,要判断智能指针的裸指 针是否为空,需要这样判断:if (my_smart_object.get())。 智能指针包含了 reset() ⽅法,如果不传递参数(或者传递 NULL),则智能指针会释放当前管理的内存。如果传递⼀个对象,则智能指 针会释放当前对象,来管理新传⼊的对象。 我们编写⼀个测试类来辅助分析: class Simple { public: Simple(int param = 0) { number = param;
std
::cout << "Simple: " << number <<
std
::endl; } ~Simple() {
std
::cout << "~Simple: " << number <<
std
::endl; } void PrintSomething() {
std
::cout << "PrintSomething: " << info_extend.c_str() <<
std
::endl; }
std
::string info_extend; int number; }; 2、
std
::auto_
ptr
std
::auto_
ptr
属于 STL,当然在 namespace
std
中,包含头⽂件 #include
便可以使⽤。
std
::auto_
ptr
能够⽅便的管理单个堆 内存对象。 我们从代码开始分析: void TestAuto
Ptr
() {
std
::auto_
ptr
my_memory(new Simple(1)); // 创建对象,输出:Simple:1 if (my_memory.get()) { // 判断智能指针是否为空 my_memory->PrintSomething(); // 使⽤ operator-> 调⽤智能指针对象中的函数 my_memory.get()->info_extend = "Addition";
C++ 语言
64,637
社区成员
250,559
社区内容
发帖
与我相关
我的任务
C++ 语言
C++ 语言相关问题讨论,技术干货分享,前沿动态等
复制链接
扫一扫
分享
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++
技术论坛(原bbs)
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
请不要发布与C++技术无关的贴子
请不要发布与技术无关的招聘、广告的帖子
请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下
试试用AI创作助手写篇文章吧
+ 用AI写文章