带引用计数的智能指针实现
来源:互联网 发布:淘宝宝贝突然没有了 编辑:程序博客网 时间:2024/05/01 08:39
自己实现的智能指针:
优点:有引用计数,可以实现指针间共享一个对象。
缺点:不能实现数组的动态分配。
auto_cptr.h:
#ifndef _AUTO_CPTR_H_#define _AUTO_CPTR_H_//#define __debug// auto_cptr.h// 带引用计数的智能指针#include <iostream>using namespace std;template <typename T>class auto_cptr{public: auto_cptr():p(0),count(0) { #ifdef __debug cout<<"auto_cptr():empty construction of auto_cptr"<<endl; #endif } template <typename U> explicit auto_cptr(U *pu):p( (T*)pu ),count(0) { if(pu) { count=new size_t; *count = 1; #ifdef __debug cout<<"auto_cptr(U *pu): construction of auto_cptr, obj="<<p<<", count="<<get_count()<<" "<<endl; #endif }else { #ifdef __debug cout<<"auto_cptr(U *pu): construction of auto_cptr, null=null"<<endl; #endif } } auto_cptr(const auto_cptr<T>& cp):p( 0 ),count(0) { copyfrom(cp); #ifdef __debug cout<<"auto_cptr(const auto_cptr<T>& cp):copy construction of auto_cptr.obj=" <<p<<", count="<<get_count()<<" "<<endl; #endif } auto_cptr<T>& operator=(const auto_cptr <T>&cp) { if(&cp==this) return *this; copyfrom(cp); #ifdef __debug cout<<"auto_cptr::operator=:operator"<<"obj="<<p<<", count="<<get_count()<<" "<<endl; #endif return *this; } ~auto_cptr() { #ifdef __debug cout<<"~auto_cptr()"<<endl; #endif minus(); } friend bool operator==(const auto_cptr<T>&p1,const auto_cptr<T>&p2) { return p1.p==p2.p && p1.count==p2.count; } friend bool operator!=(const auto_cptr<T>&p1,const auto_cptr<T>&p2) { return !(p1==p2); } T *operator->() {return &(operator*());} T &operator*() {return *p;} size_t get_count() { return count?*count:0; } T* get_pvalue() { return p; } private: void copyfrom(const auto_cptr<T> &cp) { minus(); if(cp.p) { p=cp.p; count=cp.count; ++(*count); } } void minus() { if(p==0) return; --(*count); if(*count<=0) { #ifdef __debug cout<<"auto_cptr::minus(): delete "<<endl; #endif delete p; delete count; }else { #ifdef __debug cout<<"auto_cptr::minus():not delete, p="<<p<<", count="<<get_count()<<" "<<endl; #endif } p=0; count=0; } private: T *p; size_t *count;};#endif
测试:main.c:
#include <iostream>#define __debug // 打印智能指针的调试信息#include "auto_cptr.h"using namespace std;// 测试类class A{public: A(){cout<<"empty construction"<<endl;} A(int a_){cout<<"non-empty construction"<<endl;_a=a_;} ~A(){cout<<"deconstruction"<<endl;} void display(){cout<<"A::display()"<<endl;} friend ostream &operator<<(ostream &os, const A&a); private: int _a;};ostream &operator<<(ostream &os, const A&a){ cout<<a._a; return os;}int main(){ auto_cptr<A> p1=auto_cptr<A> (new A(5) ); auto_cptr<A> p2=p1; // 拷贝构造 auto_cptr<A>p3; // 空的智能指针 p3=p2; // 智能指针的赋值 p2=auto_cptr<A>(); // 空对象赋值给p2 cout<<"指针指向的对象值: "<<*p3<<endl; // operator * cout<<"通过智能指针调用函数: "; p3->display(); // operator -> return 0;}
运行:
chen@chen-book1:~$ gcc c.cpp -lstdc++ -o cpp -gchen@chen-book1:~$ ./cppnon-empty constructionauto_cptr(U *pu): construction of auto_cptr, obj=0x9c3c008, count=1 auto_cptr(const auto_cptr<T>& cp):copy construction of auto_cptr.obj=0x9c3c008, count=2 auto_cptr():empty construction of auto_cptrauto_cptr::operator=:operatorobj=0x9c3c008, count=3 auto_cptr():empty construction of auto_cptrauto_cptr::minus():not delete, p=0x9c3c008, count=2 auto_cptr::operator=:operatorobj=0, count=0 ~auto_cptr()指针指向的对象值: 5通过智能指针调用函数: A::display()~auto_cptr()auto_cptr::minus():not delete, p=0x9c3c008, count=1 ~auto_cptr()~auto_cptr()auto_cptr::minus(): delete deconstructionchen@chen-book1:~$
- 带引用计数的智能指针实现
- shared_ptr,带引用计数的智能指针
- 带引用计数的智能指针
- 带引用计数智能指针
- 基于引用计数的智能指针实现
- 引用计数板的智能指针实现
- 带引用计数的智能指针(模板类)实现的3种方法
- 带引用计数以及就地构造功能的智能指针
- 智能指针的实现--使用引用计数实现以及原理
- 引用计数的智能指针的实现与思考
- 引用计数的智能指针的实现与思考
- 一种基于引用计数的智能指针的实现
- 一种基于引用计数机制的智能指针实现
- 一种基于引用计数机制的智能指针实现
- 关于智能指针,引用计数的学习与实现
- c++基于引用计数的智能指针实现
- 智能指针和引用计数以及String的C++实现
- 【C++】智能指针之引用计数的实现
- 大素数的应用
- Java设计模式之观察者模式
- poj1159
- ubuntu android 找不到arm-hisiv200-linux-gcc解决办法
- Study notes for Non-negative Matrix Factorization
- 带引用计数的智能指针实现
- 去除Notification Center中的通知
- MonoDroid学习笔记(五)—— 更改TextView字体颜色和背景色及Style样式的定义
- scorm 2004 datamodel 中文解释
- 多进程(父子进程)中的管道通信
- silverlight 格式转换
- 内核参数简单说明
- pipe/fifo 管道与有名管道
- fetch bulk collect into 批量效率的读取游标数据