CCI_chapter 13C++
2024-10-20 05:31:55
13.9Write a smart pointer (smart_ptr) class
template<class T>class SmartPoint{
public:
SmartPoint(T *ref){
ref_ = ref;
count_ = (unsigned int *)malloc(sizeof(unsigned int ));
*count_ = 1;
}
SmartPoint(SmartPoint<T> &sptr){
ref_ = sptr.ref_;
count_ = sptr.count_;
++(*count_);
}
SmartPoint<T>& operator =(SmartPoint<T> &sptr)
{
if(this != sptr){
ref_ = sptr.ref_;
count_ = sptr.count_;
++(*count_);
}
return *this;
}
~SmartPoint()
{
--(*count_);
if(*count_ == 0)
{
delete ref_;
free(count_);
ref_ = NULL;
count_ = NULL;
} }
T getValue(){
rturn *ref_;
}
private:
T* ref_;
unsigned int *count_;
};
最新文章
- Dijkstra搜索算法
- HTTP请求错误大全
- MVC ActionResult -- JavaScriptResult,JsonResult
- Linux学习系列之Linux入门(三)gcc学习
- eclipse4.2.1插件安装(二)之Eclipse HTML Editor
- 数缘社区上对libtom的介绍,贴过来先
- nginx日志格式
- Oracle EBS-SQL (INV-6):检查监督帐户别名处理.sql
- 一年三篇IF大于7的牛人告诉你怎么写SCI
- 【最短路】 poj 2387
- 读书笔记 effective c++ Item 25 实现一个不抛出异常的swap
- Managing Spark data handles in R
- 详解用em替换px
- set命令详解
- 使用Http-Repl工具测试ASP.NET Core 2.2中的Web Api项目
- ZOJ 3987 Numbers(Java枚举)
- Web.config的Release版本和Debug版本不一样的奥秘
- CentOS7安装OpenStack(Rocky版)-09.安装Cinder存储服务组件(控制节点)
- 中国的 Python 量化交易工具链有哪些
- Vim技能修炼教程(7) - 可视模式