namespace wang{
template<class T>
class shared_ptr{
public:
explicit shared_ptr(T *p) :
count(new int()), Myptr_(p)
{} shared_ptr(const shared_ptr<T>&other)
{
Myptr_ = other.Myptr_;
count = &(++*other.count);
} T* operator ->(){
return Myptr_;
} T& operator *()
{
return *Myptr_;
} shared_ptr<T>& operator=(shared_ptr<T> &other)
{
++*other.count;
if (this->ptr/*当前指针不为空*/ && == --*this->count)
{
delete count; //这里不是很完善,数组的情况没考虑
delete Myptr_;
}
count = other.count;
Myptr_ = other.Myptr_;
return *this;
} ~shared_ptr(){
if ( == --*(this->count))
{
delete count;
delete Myptr_;
}
} int getRef(){ return *count; }
private:
T* Myptr_;
int * count;
};
}

代码不完善,大概是这个思路,赋值,拷贝构造,析构时都要考虑引用次数

最新文章

  1. 转载《Android Adapter简单总结》
  2. LeetCode &quot;477. Total Hamming Distance&quot;
  3. Java继承和多态实例
  4. PeopleTools预警程序制作
  5. Wordpress固定链接设置
  6. 什么是HTTP及RFC
  7. EF框架引用问题
  8. python之import模块及包的调用
  9. js 画布与图片的相互转化(canvas与img)
  10. Java servlet 实现的简易购物车
  11. 中级 jQuery 了解
  12. java多线程实现主线程等待子线程执行完问题
  13. drupal7请求异常,执行时间过长的解决方法
  14. Zabbix设置自定义监控
  15. PHP - 请求阻塞,Session写阻塞
  16. Locust 基本使用
  17. HTTP传输内容的压缩
  18. 教你搭建SpringMVC框架( 附源码)
  19. Consumer [分组背包]
  20. shell运行java/Jar 脚本

热门文章

  1. ubuntu中ANT的安装和配置
  2. Java中break、continue及标签等跳转语句的使用[上]
  3. 【c++】类管理指针成员
  4. golang基础---Slice切片
  5. JSON跨域问题总结
  6. log4j记录日志 和 webAppRootKey关系
  7. JavaScript call 和apply 的理解
  8. Node.js学习笔记(六) --- Nodejs 的非阻塞 I/O、 异步、 事件驱动
  9. openlayers 各种图层,持续更新
  10. PLSQL-12.0.7.1837注册码