【c++】模拟实现boost库里的scoped_ptr
来源:互联网 发布:文件加密解密算法 编辑:程序博客网 时间:2024/05/22 10:50
//模拟实现boost下的scoped_ptr#include <iostream>#include <assert.h>using namespace std;template <class T>class scoped_ptr{private:T * px;scoped_ptr(scoped_ptr const &);scoped_ptr& operator=(scoped_ptr const &);void operator==(scoped_ptr const &)const;void operator!=(scoped_ptr const &)const;public:scoped_ptr(T *p = 0) :px(p){}//从auto_ptr获得指针的管理权scoped_ptr(std::auto_ptr<T> p) :px(p.release()){}~scoped_ptr(){delete px;}// 删除原来的指针,保存新的指针void reset(T * p = 0){assert(p == 0 || p != px);scoped_ptr<T>(p).swap(*this);}T& operator*()const{assert(px != 0);return *px;}T* operator->()const{assert(px != 0);return px;}T* get()const{return px;}void swap(scoped_ptr & b){T *tmp = b.px;b.px = px;px = tmp;}};int main(){int *p = new int(10);scoped_ptr<int> ptr(p);cout << *ptr << endl;return 0;}
0 0
- 【c++】模拟实现boost库里的scoped_ptr
- 模拟实现C++/boost库智能指针auto_ptr scoped_ptr和share_ptr
- 模拟实现boost库里的智能指针
- boost库之scoped_ptr
- 模拟实现scoped_ptr
- 【c++】模拟实现boost库下的scoped_array
- 【C++】智能指针的作用,模拟实现auto_ptr,scoped_ptr,shared_ptr,scoped_array,shared_array
- 模拟实现auto_ptr,scoped_ptr,shardptr
- 智能指针的模拟实现 auto_ptr scoped_ptr shared_ptr
- 智能指针(还缺少scoped_ptr的模拟实现)
- C++:Boost智能指针——scoped_ptr
- boost::scoped_ptr
- boost::scoped_ptr()
- boost::scoped_ptr
- boost::scoped_ptr
- boost::lambda的实现模拟
- boost库学习之 scoped_ptr scoped_array
- 智能指针:模拟实现auto_ptr,scoped_ptr,shared_ptr
- 32位和64位ORACLE下操作GOLDENGATE过程记录
- AFNetworking如何post一个json数据到服务器
- 类型安全 与 线程安全 、异常安全、事务安全
- AC自动机应用 多模式匹配 多个单词在文章中出现的次数-C语言实现
- Java-mongodb-AggregationOutput(分组、统计)
- 【c++】模拟实现boost库里的scoped_ptr
- 警察叔叔就是它!那些烦人的空格!
- 解决"Could not initialize class android.graphics.Typeface"
- POJ-2457 SPFA+路径记录
- Unity3d 着色器语法(Shader)
- Android操作系统软键盘的显示与隐藏
- Masonry介绍与使用实践(快速上手Autolayout)
- Manacher算法-求字符串中最长回文串
- Return-to-libc攻击实验