C++boost库里面有许多好用的指针,其中以shared_ptr最为复杂,也最为像普通指针,下面简单的剖析一下shared_ptr的框架。
config.h
1 #pragma once
2
3
4 //#define DISPLAY
5 #define SP_COUNTED_IMPL_XX
6 //#define SP_COUNTED_IMPL_YY
smart_ptr.h
1
2
3
4 #pragma once
5
6 #include"shared_ptr.h"
shared_ptr.h
1 #ifndef _SHARED_PTR_H
2 #define _SHARED_PTR_H
3
4 #include"./shared_count.h"
5
6 template<class T>
7 class shared_ptr
8 {
9 public:
10 shared_ptr(T *p = 0):px(p),pn(p)
11 {
12 #ifndef DISPLAY
13 cout<<"Create shared_ptr"<<endl;
14 #endif
15 }
16 ~shared_ptr()
17 {
18 #ifndef DISPLAY
19 cout<<"Free shared_ptr Object"<<endl;
20 #endif
21 }
22 private:
23 T *px; //指针
24 shared_count pn; //共享类型计数器
25 };
26
27 #endif
shared_count.h
1 #ifndef _SHARED_COUNT_H
2 #define _SHARED_COUNT_H
3
4 #include"./sp_counted_base.h"
5 #include"./sp_counted_impl_xx.h"
6 #include"./sp_counted_impl_yy.h"
7
8 class shared_count
9 {
10 private:
11 sp_counted_base *pi_; //一个父类对象 可以直接,可以用子类实现
12 public:
13 shared_count():pi_(0)
14 {
15 cout<<"Create shared_count Object"<<endl;
16 }
17 // template<class Y>
18 // shared_count(Y *p):pi_(new sp_counted_impl_xx<Y>(p))
19 template<class Y>
20 shared_count(Y *p)
21 {
22 #ifndef DISPLAY
23 cout<<"Create shared_count Object"<<endl;
24 #endif
25 //动态 定义了哪个子类 就用哪个子类去实现
26 #ifndef SP_COUNTED_IMPL_XX
27 pi_ = new sp_counted_impl_xx<Y>(p);
28 #else
29 pi_ = new sp_counted_impl_yy<Y>(p);
30 #endif
31 }
32 virtual ~shared_count()
33 {
34 #ifndef DISPLAY
35 cout<<"Free shared_count Object"<<endl;
36 #endif
37 if(pi_ != 0) //如果所指向的不为空的话
38 //调用相应的释放函数
39 pi_->realease();
40 }
41
42 };
43
44 #endif
sp_counted_base.h
1 #pragma once
2
3 #include<iostream>
4 using namespace std;
5 #include"./config.h"
6
7
8 class sp_counted_base
9 {
10 public:
11 sp_counted_base():use_count_(1)
12 {
13 #ifndef DISPLAY
14 cout<<"Create sp_counted_base Object"<<endl;
15 #endif
16 }
17 ~sp_counted_base()
18 {
19 #ifndef DISPLAY
20 cout<<"Free sp_counted_base Object"<<endl;
21 #endif
22 }
23 public:
24 virtual void dispose() = 0;
25 void realease()//引用计数器减一 如果为0 调用摧毁函数
26 {
27 if(--use_count_ == 0)
28 {
29 this->dispose();
30 }
31 }
32 private:
33 long use_count_; //父类有一个引用计数器
34 };
sp_counted_impl.h
1 #pragma once
2
3 #include"./sp_counted_base.h"
4
5 template<class X>
6 class sp_counted_impl_xx:public sp_counted_base
7 {
8 private:
9 X * px_; //子类有相应的指针
10 public:
11 sp_counted_impl_xx(X *px):px_(px)
12 {
13 #ifndef DISPLAY
14 cout<<"Create sp_counted_impl_xx Object"<<endl;
15 #endif
16 }
17 ~sp_counted_impl_xx()
18 {
19 #ifndef DISPLAY
20 cout<<"Free sp_counted_impl_xx Object"<<endl;
21 #endif
22 delete px_;
23 }
24 virtual void dispose()
25 {
26 // delete px_;
27 #ifndef DISPLAY
28 cout<<"THis is sp_counted_impl_xx:dispose()"<<endl;
29 #endif
30 delete this;
31 }
32 }
33 ;
sp_counted_impl_yy.h
1 #pragma once
2
3 #include"./sp_counted_base.h"
4
5 template<class X>
6 class sp_counted_impl_yy:public sp_counted_base
7 {
8 private:
9 X * px_;
10 public:
11 sp_counted_impl_yy(X *px):px_(px)
12 {
13 #ifndef DISPLAY
14 cout<<"Create sp_counted_impl_yy Object"<<endl;
15 #endif
16 }
17 ~sp_counted_impl_yy()
18 {
19 #ifndef DISPLAY
20 cout<<"Free sp_counted_impl_yy Object"<<endl;
21 #endif
22 delete px_;
23 }
24 virtual void dispose()
25 {
26 // delete px_;
27 #ifndef DISPLAY
28 cout<<"This is sp_counted_impl_yy:dispose()"<<endl;
29 #endif
30 delete this;
31 }
32 }
33 ;
Main.cpp
1 #include<iostream>
2 #include"./smart_ptr.h"
3 using namespace std;
4
5 int main()
6 {
7 int *p = new int(10);
8 shared_ptr<int> pa(p);
9 return 0;
10 }