智能指针类的实现
来源:互联网 发布:苹果官方申请解网络锁 编辑:程序博客网 时间:2024/06/05 20:20
#include <iostream>using namespace std;template <typename T>class SmartPointer{public:SmartPointer(T *p=0):poin(p),use(new size_t(1)){}SmartPointer(const SmartPointer &p){if(this!=&p){poin = p.poin;use = p.use;++(*use);}}SmartPointer& operator=(const SmartPointer &tmp){if(poin==tmp.poin)return *this;++*tmp.use;if(--*use==0){cout<<"析构函数被调用了"<<endl;delete poin;delete use;}poin = tmp.poin;use = tmp.use;return *this;}T& operator*(){cout<<"get in"<<endl;return *poin;}~SmartPointer(){if(--(*use)==0){cout<<"析构函数调用"<<endl;delete poin;delete use;}} void Print(){cout<<*use<<" "<<*poin<<endl;}private:T *poin;size_t *use;};int main(){SmartPointer<int> a(new int(4));a.Print();SmartPointer<int> c;c=a;cout<<"a........."<<endl;a.Print();cout<<"c........."<<endl;c.Print();return 0;}
阅读全文
0 0
- 智能指针的类实现
- 智能指针类的实现
- 智能指针的实现
- 智能指针的实现
- 智能指针的实现
- 智能指针的实现
- 智能指针的实现
- 智能指针的简单实现
- C++ 智能指针的实现
- 智能指针的部分实现
- 智能指针的一种实现
- c++智能指针的实现
- 智能指针的一种实现
- 智能指针的简单实现
- C++智能指针的实现
- 智能指针的实现机理
- 【C++】智能指针的实现
- 智能指针的实现尝试
- BlockingQueue
- Django第一个web程序
- Spring事务属性
- Linux权限控制的基本原理
- bootstrap 样式的左侧菜单树
- 智能指针类的实现
- [leetcode] Median of Two Sorted Arrays
- Git指令总结
- 一些网址记录
- Codeforces 754 D Fedor and coupons
- 【算法题】不等式数列
- java反射基础知识总结
- mysql 安装、建表及允许远程连接,及django连接mysql
- wlan没有有效的ip配置 win10