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

system/core/include/utils/RefBase.h

system/core/libutils/RefBase.cpp

system/core/include/utils/StrongPointer.h

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

 #include <iostream>

 using namespace std;

 template<typename T>
class sp {
public:
inline sp() {};
sp(T* other);
sp(const sp<T>& other);
template<typename U> sp(U* other);
template<typename U> sp(const sp<U>& other); ~sp(); sp& operator = (T* other);
sp& operator = (const sp<T>& other);
template<typename U> sp& operator = (const sp<U>& other);
template<typename U> sp& operator = (U* other);
}; template<typename T>
sp<T>::sp(T* other) {
cout << "enter sp(T* other) " << endl;
} template<typename T>
sp<T>::sp(const sp<T>& other) {
cout << "enter sp(const sp<T>& other) " << endl;
} template<typename T> template<typename U>
sp<T>::sp(U* other) {
cout << "enter sp(U *other) " << endl;
} template<typename T> template<typename U>
sp<T>::sp(const sp<U>& other) {
cout << "sp(const sp<U& other>)" << endl;
} template<typename T>
sp<T>::~sp() {
} template<typename T>
sp<T>& sp<T>::operator =(T* other) {
cout << "enter operator =(T* other) " << endl;
return *this;
} template<typename T>
sp<T>& sp<T>::operator =(const sp<T>& other) {
cout << "enter operator = (const sp<T>& other)" << endl;
return *this;
} template<typename T> template<typename U>
sp<T>& sp<T>::operator =(const sp<U>& other) {
cout << "operator = (const sp<U>& other)" << endl;
return *this;
} template<typename T> template<typename U>
sp<T>& sp<T>::operator =(U* other) {
cout << "operator = (U* other)" << endl;
return *this;
} class Peng{
}; class Dong {
}; int main(int argc, const char *argv[])
{
Peng *p = new Peng();
Dong *d = new Dong(); cout << "---- sp<Peng> q = p ----" << endl;
sp<Peng> q = p; cout << "\n---- sp<Peng> r(p) ----" << endl;
sp<Peng> r(p); cout << "\n---- sp<Peng> b(r) ----" << endl;
sp<Peng> b(r); cout << "\n---- sp<Peng> t; ----" << endl;
sp<Peng> t;
cout << "\n---- t = p ----" << endl;
t = p; cout << "\n---- sp<Peng> a; ----" << endl;
sp<Peng> a;
cout << "\n---- a = t ----" << endl;
a = t; Dong *c = new Dong();
cout << "\n---- sp<Dong> e = c ----" << endl;
sp<Dong> e = c;
cout << "\n---- a = e ----" << endl;
a = e;
cout << "\n---- a = c ----" << endl;
a = c; cout << "\n---- sp<Dong> e1(e) ----" << endl;
sp<Peng> e1(e); cout << endl << endl;
return ;
}

下面是在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>)

完。

最新文章

  1. Java并发之原子变量和原子引用与volatile
  2. [Protobuf] Mac系统下安装配置及简单使用
  3. 几种垂直居中的方式及CSS图片替换技术
  4. iOS 项目改名~~~~~
  5. super用法和继承中的构造方法
  6. CentOS卸载OpenJDK并安装Sun JDK
  7. (08)odoo继承机制
  8. 数据库知识(主要基于Oracle,Sql可参考)
  9. 类、对象以及jvm运行内存解析
  10. [关于SQL]查询成绩都大于80分的学生
  11. 0基础学习ios开发笔记第一天
  12. CentOS Device eth0 does not seem to be present 解决方案
  13. 为什么针对XML的支持不够好?如何改进?
  14. Deep learning From Image to Sequence
  15. 裸板驱动总结(makefile+lds链接脚本+裸板调试)
  16. Apache Spark 内存管理详解(转载)
  17. M1卡知识点描述
  18. js实现文件的上传和输出,拖拽上传图片
  19. LY.JAVA.DAY12.String类
  20. sourcetree 跳过注册

热门文章

  1. 从一段字符串中去除数字的shell方法
  2. [ CodeVS冲杯之路 ] P2952
  3. cdp协议通信并发编程基础之进程
  4. Google I/O完整盘点,这才是地球上最「性感」的发布会
  5. servlet+forward和direct区别
  6. 1.tornado实现高并发爬虫
  7. js一段小代码(浏览器用alert,否则用console)
  8. C#MD5加密和DES加密解密算法
  9. 【python】ipython与python的区别
  10. leetcode83 Remove Duplicates from Sorted List