Android 智能指针学习 一

Android5.1 中智能指针涉及的文件如下:

system/core/include/utils/RefBase.h

system/core/libutils/RefBase.cpp

system/core/include/utils/StrongPointer.h

在学习Android的智能指针时,对于模板的使用不太清楚,于是我将sp类精简了一下,大概看一下在赋值和初始化的时候的函数调用关系:

  1 #include <iostream>
  2 
  3 using namespace std;
  4 
  5 template<typename T>
  6 class sp {
  7 public:
  8     inline sp() {};
  9     sp(T* other);
 10     sp(const sp<T>& other);
 11     template<typename U> sp(U* other);
 12     template<typename U> sp(const sp<U>& other);
 13 
 14     ~sp();
 15 
 16     sp& operator = (T* other);
 17     sp& operator = (const sp<T>& other);
 18     template<typename U> sp& operator = (const sp<U>& other);
 19     template<typename U> sp& operator = (U* other);
 20 };
 21 
 22 template<typename T>
 23 sp<T>::sp(T* other) {
 24             cout << "enter sp(T* other) " << endl;
 25 }
 26 
 27 template<typename T>
 28 sp<T>::sp(const sp<T>& other) {
 29         cout << "enter sp(const sp<T>& other) " << endl;
 30 }
 31 
 32 template<typename T> template<typename U>
 33 sp<T>::sp(U* other) {
 34         cout << "enter sp(U *other) " << endl;
 35 }
 36 
 37 template<typename T> template<typename U>
 38 sp<T>::sp(const sp<U>& other) {    
 39     cout << "sp(const sp<U& other>)" << endl;
 40 }
 41 
 42 template<typename T>
 43 sp<T>::~sp() {
 44 }
 45 
 46 template<typename T>
 47 sp<T>& sp<T>::operator =(T* other) {
 48     cout << "enter operator =(T* other) " << endl;
 49     return *this;
 50 }
 51 
 52 
 53 template<typename T>
 54 sp<T>& sp<T>::operator =(const sp<T>& other) {
 55     cout << "enter operator = (const sp<T>& other)" << endl;
 56     return *this;
 57 }
 58 
 59 template<typename T> template<typename U>
 60 sp<T>& sp<T>::operator =(const sp<U>& other) {
 61     cout << "operator = (const sp<U>& other)" << endl;
 62     return *this;
 63 }
 64 
 65 template<typename T> template<typename U>
 66 sp<T>& sp<T>::operator =(U* other) {
 67     cout << "operator = (U* other)" << endl;
 68     return *this;
 69 }
 70 
 71 class Peng{
 72 };
 73 
 74 class Dong {
 75 };
 76 
 77 int main(int argc, const char *argv[])
 78 {
 79     Peng *p = new Peng();
 80     Dong *d = new Dong();
 81 
 82     cout << "---- sp<Peng> q = p ----" << endl;
 83     sp<Peng> q = p;
 84 
 85     cout << "
---- sp<Peng> r(p) ----" << endl;
 86     sp<Peng> r(p);
 87 
 88     cout << "
---- sp<Peng> b(r) ----" << endl;
 89     sp<Peng> b(r);
 90 
 91     cout << "
---- sp<Peng> t; ----" << endl;
 92     sp<Peng> t;
 93     cout << "
---- t = p ----" << endl;
 94     t = p;
 95 
 96     cout << "
---- sp<Peng> a; ----" << endl;
 97     sp<Peng> a;
 98     cout << "
---- a = t ----" << endl;
 99     a = t;
100 
101     Dong *c = new Dong();
102     cout << "
---- sp<Dong> e = c ----" << endl;
103     sp<Dong> e = c;
104     cout << "
---- a = e ----" << endl;
105     a = e;
106     cout << "
---- a = c ----" << endl;
107     a = c;
108 
109     cout << "
---- sp<Dong> e1(e) ----" << endl;
110     sp<Peng> e1(e);
111 
112     cout << endl << endl;
113     return 0;
114 }

下面是在PC机上的运行结果:

pengdl@pengdl-HP:~/work/study/c++$ ./a.out 
---- sp<Peng> q = p ----
enter sp(T* other) 

---- sp<Peng> r(p) ----
enter sp(T* other) 

---- sp<Peng> b(r) ----
enter sp(const sp<T>& other) 

---- sp<Peng> t; ----

---- t = p ----
enter operator =(T* other) 

---- sp<Peng> a; ----

---- a = t ----
enter operator = (const sp<T>& other)

---- sp<Dong> e = c ----
enter sp(T* other) 

---- a = e ----
operator = (const sp<U>& other)

---- a = c ----
operator = (U* other)

---- sp<Dong> e1(e) ----
sp(const sp<U& other>)

完。

原文地址:https://www.cnblogs.com/pengdonglin137/p/4717488.html