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. template<typename T>
  53. sp<T>& sp<T>::operator =(const sp<T>& other) {
  54. cout << "enter operator = (const sp<T>& other)" << endl;
  55. return *this;
  56. }
  57.  
  58. template<typename T> template<typename U>
  59. sp<T>& sp<T>::operator =(const sp<U>& other) {
  60. cout << "operator = (const sp<U>& other)" << endl;
  61. return *this;
  62. }
  63.  
  64. template<typename T> template<typename U>
  65. sp<T>& sp<T>::operator =(U* other) {
  66. cout << "operator = (U* other)" << endl;
  67. return *this;
  68. }
  69.  
  70. class Peng{
  71. };
  72.  
  73. class Dong {
  74. };
  75.  
  76. int main(int argc, const char *argv[])
  77. {
  78. Peng *p = new Peng();
  79. Dong *d = new Dong();
  80.  
  81. cout << "---- sp<Peng> q = p ----" << endl;
  82. sp<Peng> q = p;
  83.  
  84. cout << "\n---- sp<Peng> r(p) ----" << endl;
  85. sp<Peng> r(p);
  86.  
  87. cout << "\n---- sp<Peng> b(r) ----" << endl;
  88. sp<Peng> b(r);
  89.  
  90. cout << "\n---- sp<Peng> t; ----" << endl;
  91. sp<Peng> t;
  92. cout << "\n---- t = p ----" << endl;
  93. t = p;
  94.  
  95. cout << "\n---- sp<Peng> a; ----" << endl;
  96. sp<Peng> a;
  97. cout << "\n---- a = t ----" << endl;
  98. a = t;
  99.  
  100. Dong *c = new Dong();
  101. cout << "\n---- sp<Dong> e = c ----" << endl;
  102. sp<Dong> e = c;
  103. cout << "\n---- a = e ----" << endl;
  104. a = e;
  105. cout << "\n---- a = c ----" << endl;
  106. a = c;
  107.  
  108. cout << "\n---- sp<Dong> e1(e) ----" << endl;
  109. sp<Peng> e1(e);
  110.  
  111. cout << endl << endl;
  112. return ;
  113. }

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

  1. pengdl@pengdl-HP:~/work/study/c++$ ./a.out
  2. ---- sp<Peng> q = p ----
  3. enter sp(T* other)
  4.  
  5. ---- sp<Peng> r(p) ----
  6. enter sp(T* other)
  7.  
  8. ---- sp<Peng> b(r) ----
  9. enter sp(const sp<T>& other)
  10.  
  11. ---- sp<Peng> t; ----
  12.  
  13. ---- t = p ----
  14. enter operator =(T* other)
  15.  
  16. ---- sp<Peng> a; ----
  17.  
  18. ---- a = t ----
  19. enter operator = (const sp<T>& other)
  20.  
  21. ---- sp<Dong> e = c ----
  22. enter sp(T* other)
  23.  
  24. ---- a = e ----
  25. operator = (const sp<U>& other)
  26.  
  27. ---- a = c ----
  28. operator = (U* other)
  29.  
  30. ---- sp<Dong> e1(e) ----
  31. sp(const sp<U& other>)

完。

Android 智能指针学习 一的更多相关文章

  1. android 智能指针的学习先看邓凡平的书扫盲 再看前面两片博客提升

    android 智能指针的学习先看邓凡平的书扫盲 再看前面两片博客提升

  2. Qt 智能指针学习(7种指针)

    Qt 智能指针学习 转载自:http://blog.csdn.net/dbzhang800/article/details/6403285 从内存泄露开始? 很简单的入门程序,应该比较熟悉吧 ^_^ ...

  3. Android结构分析Android智能指针(两)

    笔者:刘蒿羽 博客:http://blog.csdn.net/liuhaoyutz Android版本号:4.4.2 在上一篇文章中,我们分析了Android智能指针中的强指针sp,本文我们来分析弱指 ...

  4. Android智能指针SP WP使用方法介绍

    Android手机操作系统既然是开源的操作系统.那么在具体的文件夹中就会存放着各种相关功能的开源代码.我们在使用的时候可以根据这些源代码进行相应的修改就能轻松的完成我们所需的功能.在这里大家就一起来看 ...

  5. Android智能指针sp wp详解

    研究Android的时候,经常会遇到sp.wp的东西,网上一搜,原来是android封装了c++中对象回收机制.说明:1. 如果一个类想使用智能指针,那么必须满足下面两个条件:    a. 该类是虚基 ...

  6. Qt 智能指针学习(7种QT的特有指针)

    从内存泄露开始? 很简单的入门程序,应该比较熟悉吧 ^_^ #include <QApplication> #include <QLabel> int main(int arg ...

  7. Qt 智能指针学习(7种QT智能指针和4种std智能指针)

    从内存泄露开始? 很简单的入门程序,应该比较熟悉吧 ^_^ #include <QApplication> #include <QLabel> int main(int arg ...

  8. Qt 智能指针学习

    原地址:http://blog.csdn.net/dbzhang800/article/details/6403285 从内存泄露开始? 很简单的入门程序,应该比较熟悉吧 ^_^ #include & ...

  9. [转]Qt 智能指针学习

    从内存泄露开始? 很简单的入门程序,应该比较熟悉吧 ^_^ #include <QApplication> #include <QLabel> int main(int arg ...

随机推荐

  1. 记录一次Nginx跳转报错的问题

    错误信息如下: An error occurred. Sorry, the page you are looking for is currently unavailable. Please try ...

  2. RGB颜色原理

    参考:http://www.cnblogs.com/harrytian/archive/2012/12/12/2814210.html 工作中经常和颜色打交道,但却从来没有从原理上了解一下,这篇文章希 ...

  3. AE特效-与MAYA的结合、制作音乐舞蹈太极动作

    http://blog.sina.com.cn/s/blog_a439a2670101fbkk.html AE特效-与MAYA的结合.制作音乐舞蹈太极动作 (2013-07-24 14:44:12) ...

  4. vbs登陆网站

    Option Explicit Dim objIE Set objIE = CreateObject("InternetExplorer.Application") objIE.V ...

  5. bp神经网络模型推导与c语言实现(转载)

    转载出处:http://www.cnblogs.com/jzhlin/archive/2012/07/28/bp.html BP 神经网络中的 BP 为 Back  Propagation 的简写,最 ...

  6. 表单文件上传,ajax文件上传

    原创链接:http://www.cnblogs.com/yanqin/p/5345562.html html代码  index.jsp(表单文件上传) <form action="sh ...

  7. [译]lambda表达式对 SAM (单个抽象方法类)type的处理方式

    在阅读Venkat Subramaniam的著作<Functional Programming in Java> 之后,方法模式和lambda完美结合让我印象深刻. 这种模式经常用作数据源 ...

  8. 区块链开发(六)truffle使用入门和testrpc安装

    在上篇博文中我们已经成功安装了truffle及所需相关环境,此篇就简单介绍一些truffle的使用及目录结构等. 简介truffle和testrpc truffle是本地的用来编译.部署智能合约的工具 ...

  9. ORM-老师信息系统

    老师信息管理 思考 三种方式创建多对多外键方式及其优缺点. 通过外键创建 (自定义第三张表,通过外键与其他两张表关联  但是不能用Django ORM 多对多操作的语法) class Class(mo ...

  10. debian 更换sh的默认链接为bash

    https://blog.csdn.net/mudongliangabcd/article/details/43458895