智能指针与弱引用详解
2013-09-26 09:28
633 查看
Template <typename T>
class SmartPtr{
public:
SmartPtr(T *p = 0):ptr(p){count = new int(1) ;}// 第一次创建的时候,引数肯定是1
SmartPtr(const SmartPtr & rhs):ptr(rhs.ptr),count(rhs.count){++*rhs.count ;}
T &operator*(){return *ptr}
T* operator->(){return ptr}
SmartPtr &operator=(const SmartPtr & rhs){
if(ptr == rhs.ptr)
return *this ;
if(--*count == 0){
delete ptr ;
delete count ;
}
++*rhs.count ;
count = rhs.count ;
ptr = rhs.ptr ;
}
~SmartPtr(){
if(--*count==0)
delete ptr ;
delete count ;
}
private:
T *ptr ;
int *count ;
相关文章推荐
- C++双指针与指针引用详解例程
- 因为滥用智能指针导致的引用计数错误
- C++文本查询程序 定义类管理数据 用引用共享数据 不用智能指针 C++Primer练习12.27
- 关于引用计数型智能指针的一些想法
- C++ 智能指针详解
- C++ 智能指针详解
- C++智能指针使用详解
- 一种基于引用计数机制的智能指针实现
- C++智能指针详解
- C++ 智能指针详解
- C++ 智能指针详解
- 智能指针的死穴 -- 循环引用
- C++ 智能指针——简单实现以及循环引用问题
- C++ 值传递、指针传递、引用传递详解
- C++ 智能指针详解
- 详解c++指针的指针和指针的引用
- C++中的auto_ptr智能指针的作用及使用方法详解
- c++智能指针用法详解
- 详解c++指针的指针和指针的引用
- C++ 值传递、指针传递、引用传递详解