145.智能指针原理

  1 #include<iostream>
  2 
  3 using namespace std;
  4 
  5 //命名空间
  6 namespace smart_point
  7 {
  8         //智能指针计数类
  9     class smart_count
 10     {
 11     public:
 12                 //构造函数
 13         smart_count(int c=0):use_count(c)
 14         {
 15 
 16         }
 17         ~smart_count()
 18         {
 19 
 20         }
 21                 //计数加一
 22         int addref()
 23         {
 24             return ++use_count;
 25         }
 26                 //计数减一
 27         int release()
 28         {
 29             return --use_count;
 30         }
 31 
 32     private:
 33         int use_count;
 34 
 35     };
 36     
 37 
 38         //智能指针
 39     template <class T>
 40     class smart_ptr
 41     {
 42     public:
 43         //构造函数
 44         smart_ptr(T*ptr=nullptr): p(ptr),u(new smart_count)
 45         {
 46 
 47         }
 48                 //构造函数
 49         smart_ptr() :p(nullptr), u(nullptr)
 50         {
 51 
 52         }
 53                 //如果是引用其他指针则使用数量加一,不能被释放
 54         smart_ptr(const smart_ptr<T> & s)
 55         {
 56             this->p = s.p;
 57             this->u = s.u;//浅拷贝
 58 
 59             if (u)
 60             {
 61                 u->addref();//引用加1
 62 
 63             }
 64 
 65         }
 66                 //如果是等于其他指针则使用数量加一,不能被释放
 67         void operator =(const smart_ptr<T> & s)
 68         {
 69                         //如果被用=号赋值,则先判断能否被删除
 70                         //如果能销毁则销毁,然后赋值
 71             if (p && u->release() <= 0)
 72             {
 73                 delete p;
 74                 delete u;
 75             
 76             }
 77 
 78             this->p = s.p;
 79             this->u = s.u;//浅拷贝
 80 
 81             if (u)
 82             {
 83                 u->addref();//引用加1
 84 
 85             }
 86         }
 87                 //取内容
 88         T & operator * ()
 89         {
 90             return *p;
 91         }
 92         T operator ->()//具备指针的*,->
 93         {
 94             return p;
 95         }
 96         T * get()
 97         {
 98             return p;//获取内部指针
 99         }
100         void rest(T *ptr)
101         {
102             if (p && u->release() <= 0)
103             {
104                 delete p;
105                 delete u;
106 
107             }//销毁,赋值
108 
109             p = ptr;
110             if (p)
111             {
112                 u = new smart_count(1);
113             }
114             else
115             {
116                 u = null;
117             }
118 
119 
120         }
121         void rest(const smart_ptr<T> & s)
122         {
123             if (p && u->release() <= 0)
124             {
125                 delete p;
126                 delete u;
127 
128             }//销毁,赋值
129 
130             p = s.p;
131             u = s.u;
132             if (u)
133             {
134                 u->addref();
135             }
136 
137 
138 
139         }
140         ~smart_ptr()
141         {
142             //注意析构。删除数据,引用
143             if (p && u->release() <= 0)
144             {
145                 delete p;
146                 delete u;
147                 p = nullptr;//析构
148             }
149 
150 
151         }
152         //重载放在内部
153         template< class U>
154         inline  bool operator ==( const smart_ptr<U> & t)
155         {
156             return (this->get() == t.get());
157         }
158         template< class U>
159         inline  bool operator !=( const smart_ptr<U> & t)
160         {
161             return (this->get() != t.get());
162         }
163 
164     private:
165         T *p;
166         smart_count *u;
167     };
168 
169 
170 
171 
172 
173 }
174 
175 
176 
177 void main()
178 {
179 
180     while (1)
181     {
182         smart_point::smart_ptr<int> p(new int[1024 * 1024 * 100]);
183     }
184 
185 
186     cin.get();
187 }

 

posted @ 2018-04-04 21:54  喵小喵~  阅读(218)  评论(0编辑  收藏  举报