boost smart_ptr -> scoped_ptr
来源:互联网 发布:windows7内存优化 编辑:程序博客网 时间:2024/05/16 05:31
scoped_ptr P78
C++98 的标准“自动指针”:std::auto_ptr
#include <boost/smart_ptr.hpp> using namespace boost;
只能在本作用域内使用,不希望被转让。
a. 不支持比较操作
b. .swap()交换两个scoped_ptr保存的原始指针,高效操作。
c. 缺陷:不能用作容器的元素
scoped_prt<string> sp(new string("text")); cout<< *sp <<endl; cout<< sp->size() <<endl;
#include <iostream> #include <boost/assert.hpp> #include <boost/smart_ptr.hpp> using namespace std; using namespace boost; struct posix_file //一个示范性质的文件类 { posix_file(const char* file_name) //构造函数打开文件 { cout << "Open file:" << file_name << endl; } ~posix_file() { cout << "close file" << endl; } }; int main(int argc, char *argv[]) { scoped_ptr<int> p(new int); if(p) //在bool语境中测试指针是否有效 { *p = 100; cout << *p << endl; } p.reset(); //reset()置空scoped_ptr,仅仅是演示 assert(p == 0); //p不持有任何指针 if(!p) { cout << "scoped_ptr == null" << endl; } //文件类的scoped_ptr //将在离开任用域时自动析构,从而关闭文件释放资源 scoped_ptr<posix_file> fp(new posix_file("/tmp/a.txt")); return 0; }
0 0
- Boost.smart_ptr.scoped_ptr--1
- boost smart_ptr -> scoped_ptr
- boost.smart_ptr-智能指针scoped_ptr
- boost.smart_ptr-智能指针scoped_ptr
- boost.smart_ptr-智能指针scoped_ptr
- boost.smart_ptr-智能指针scoped_ptr
- Boost源码剖析--<boost/smart_ptr/scoped_ptr.hpp>
- 智能指针Boost.smart_ptr学习2--scoped_ptr
- smart_ptr学习之scoped_ptr
- Boost Smart_ptr
- Boost::smart_ptr
- boost::smart_ptr
- Boost库高效内存管理——smart_ptr库——scoped_ptr/scoped_array
- boost::scoped_ptr
- boost::scoped_ptr()
- boost::scoped_ptr
- boost::scoped_ptr
- Boost.smart_ptr.shared_ptr--2
- Node.js学习笔记(一)基础介绍
- FluentNHibernate
- Android Studio 切换eclipse 快捷键
- 支持向量机(SVM)算法的Python实现
- JAVA系书单
- boost smart_ptr -> scoped_ptr
- 卡片式布局 MD风格设计 卡片式背景
- nodejs-fs模块
- HttpUrlConnection使用时遇到的问题
- CART决策树的Python实现
- ElasticSearch安装
- 1119. Pre- and Post-order Traversals (30)
- 欢迎使用CSDN-markdown编辑器
- DB2中storage group的概念,以及创建各种类型的表空间