boost smart pointer
1. boost::scoped_ptr is a smart pointer that is the sole owner of a dynamically allocated object and cannot be copied or moved.
#include <boost/scoped_ptr.hpp>
#include <iostream> int main() {
boost::scoped_ptr<int> p(new int());
std::cout << *p << std::endl; p指针地址
p.reset(new int()); reset()释放之前的对象,重新设置新的对象
std::cout << *p.get() << std::endl; get()获取指针地址
p.reset();
std::cout << std::boolalpha << static_cast<bool>(p) << std::endl; 返回p的bool值,此时为false
return ;
}
A smart pointer of type boost::scoped_ptr can't transfer ownership of an object. Once initialized an address, the dynamically allocated object is released when
the destructor is executed or when the member function reset() is called.
2. boost::scoped_array is used like boost::scoped_ptr. The crucial difference is that the destructor of boost::scoped_array uses the operator delete[] to release the contained object. Because this operator only applies to arrays, a boost::scoped_array must be initialized with the address of a dynamically allocated array.
#include <boost/scoped_array.hpp> int main() {
boost::scoped_array<int> p(new int[]);
*p.get() = ;
p[] = ;
p.reset(new int[]);
return ;
}
3. The smart pointer boost::shared_ptr is similar to boost::scoped_ptr. The key difference is that boost::shared_ptr is not necessarily the exclusive owner of an object. Onwership can be shared with other smart pointers of type boost::shared_ptr. In such a case, the shared object is not released until the last copy of the shared pointer referencing the object is destroyed. The smart pointer can be copied.
#include <boost/shared_ptr.hpp>
#include <iostream> int main() {
boost::shared_ptr<int> p1(new int());
std::cout << *p1 << std::endl;
boost::shared_ptr<int> p2(p1);
p1.reset(new int());
std::cout << *p1.get() << std::endl;
p1.reset();
std::cout << std::boolalpha << static_cast<bool>(p2) << std::endl;
return ;
}
输出为:
1
2
true
When reset() is called on p1, a new int object is anchored in p1. This doesn't mean that the existing int object is destroyed. Since it is also anchored in p2, it continues to exist. boost::shared_ptr uses a reference counter internally. Only when boost::shared_ptr detects that the last copy of the smart pointer has been destroyed is the contained object released with delete.
4. boost::shared_array calls delete[] in the destructor, this smart pointer can be used for arrays.
#include <boost/shared_array.hpp>
#include <iostream> int main() {
boost::shared_array<int> p1(new int[]);
{
boost::shared_array<int> p2(p1);
p2[] = ;
}
std::cout << p1[] << std::endl;
return ;
}
the smart pointers p1 and p2 share ownership of the dynamically allocated int array. When the array in p2 is accessed with operator[] to store the number 1, the same array is accessed with p1. Thus, the example writes 1 to standard output.
5. boost::weak_ptr only make sense if used in conjunction with boost::shared_ptr.
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <thread>
#include <functional>
#inclue <iostream> void reset(boost::shared_ptr<int>& sh) {
sh.reset();
} void print(boost::weak_ptr<int>& w) {
boost::shared_ptr<int> sh = w.lock();
if (sh) {
std::cout << *sh << std::endl;
}
} int main() {
boost::shared_ptr<int> sh(new int());
boost::weak_ptr<int> w(sh);
std::thread t1(reset, std:ref(sh));
std::thread t2(print, std::ref(w));
t1.join();
t2.join();
return ;
}
boost::weak_ptr must be initialized with a boost::shared_ptr. Its most important member function is lock(). lock() returns a boost::shared_ptr that shares ownership with the shared pointer used to initialize the weak pointer. In case the shared pointer is empty, the returned pointer will be empty as well.
boost smart pointer的更多相关文章
- 理解smart pointer之三:unique_ptr
unique_ptr最先在boost中被定义,后来被C++标准委员会选中为C++11的feature之一. std::unique_ptr is a smart pointer that retain ...
- c++(smart pointer)
(一)首先对智能指针有一些概念性的了解 **********本部分内容摘自开源中国社区http://my.oschina.net/u/158589/blog/28994******** 1.什么是智能 ...
- Smart pointer 智能指针小总结
Smart pointer line 58之后smart pointer里的计数已经是0,所以会真正释放它引用的对象,调用被引用对象的析构函数.如果继续用指针访问,会出现如下图的内存访问异常.所以说如 ...
- 到底有多少种智能指针(smart pointer)
最近Qt的blog总结了到底有多少种smart pointer, 下面是一个简要的介绍: 1. QPointer :提供对指针的保护,当一个指针被删除以后,再使用不会造成野指针或者指针溢出.比如 ...
- [CareerCup] 13.8 Smart Pointer 智能指针
13.8 Write a smart pointer class. A smart pointer is a data type, usually implemented with templates ...
- Why do we need smart pointer and how to implement it.
Here are two simple questions. Problem A #include <string> include <iostream> using name ...
- c++ smart pointer
智能指针(smart pointer)是存储指向动态分配(堆)对象指针的类,用于生存期控制,能够确保自动正确的销毁动态分配的对象,防止内存泄露.它的一种通用实现技术是使用引用计数(reference ...
- c/c++ 标准库 智能指针( smart pointer ) 是啥玩意儿
标准库 智能指针( smart pointer ) 是啥玩意儿 一,为什么有智能指针??? c++程序员需要自己善后自己动态开辟的内存,一旦忘了释放,内存就泄露. 智能指针可以帮助程序员"自 ...
- C++ smart pointer智能指针
在C++中,程序员可以直接操作内存,给编程增加了不少的灵活性.但是灵活性是有代价的,程序员必须负责自己负责释放自己申请的内存,否则就会出现内存泄露.智能指针就是为了解决这个问题而存在的.它和其他指 ...
随机推荐
- 如何在MaxCompute上处理存储在OSS上的开源格式数据
0. 前言 MaxCompute作为使用最广泛的大数据平台,内部存储的数据以EB量级计算.巨大的数据存储量以及大规模计算下高性能数据读写的需求,对于MaxCompute提出了各种高要求及挑战.处在大数 ...
- Visual Studio 2008 附加进程调试
关于附加进程调试的问题: 在项目当中经常使用“附加到进程”来调试项目,感觉挺方便的.我们做的项目通常都会发布到IIS(特别是B/S),一可以直接通过地址栏输入地址就可以运行项目,不必去使用开发工具来打 ...
- delphi 解决RichViewEdit乱码问题
⑴ 设置RichViewEdit下面的几个属性: ① RTFReaderProperties → ParaStyleMode → rvrsAddIfNeeded ② RTFReaderProperti ...
- intellijidea查看git窗口
version control null
- php面试专题---5、流程控制考点
php面试专题---5.流程控制考点 一.总结 一句话总结: 看代码不要先看函数里面的内容,要用的时候再去看:注意静态,注意变量作用域,php中的内置函数需要去归类总结,就是太容易忘记了 1.写出如下 ...
- html中map标签和area标签的应用(总结)
html中map标签和area标签的应用(总结) 一.总结 一句话总结: html中map标签和area标签和组成图片地图,在前端优化中可以减少http请求 1.map标签的用途是什么? 图片地图:是 ...
- linux设备驱动第三篇:写一个简单的字符设备驱动
在linux设备驱动第一篇:设备驱动程序简介中简单介绍了字符驱动,本篇简单介绍如何写一个简单的字符设备驱动.本篇借鉴LDD中的源码,实现一个与硬件设备无关的字符设备驱动,仅仅操作从内核中分 ...
- KMP算法中next数组的构建
记得初学$kmp$的时候 老师让大家把它直接背下来 然而不理解的话 不仅调试起来比较慢 很多题目也难往$kmp$上想 ----------------------------------------- ...
- HTML最全标签
一.HTML标记 标签:!DOCTYPE 说明:指定了 HTML 文档遵循的文档类型定义(DTD). 标签:a 说明:标明超链接的起始或目的位置. 标签:acronym 说明:标明缩写词. ...
- Ajax的那点事
Ajax中什么是同步.异步? 同步:就是用户填写完信息之后,全部提交给服务器,等待服务器的回应,是一次性全部的. 异步:当用户填写完一条信息之后,这条信息会自动向服务器提交,然后服务器响应客户端,在此 ...