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>)

完。

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. 【BZOJ1146】【CTSC2008】网络管理 [整体二分]

    网络管理 Time Limit: 50 Sec  Memory Limit: 162 MB[Submit][Status][Discuss] Description M公司是一个非常庞大的跨国公司,在 ...

  2. codechef T2 Chef and Sign Sequences

    CHEFSIGN: 大厨与符号序列题目描述 大厨昨天捡到了一个奇怪的字符串 s,这是一个仅包含‘<’.‘=’和‘>’三种比较符号的字符串. 记字符串长度为 N,大厨想要在字符串的开头.结尾 ...

  3. BZOJ1082_栅栏_C++

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1082 题解:http://www.cnblogs.com/hadilo/p/5924546.h ...

  4. Python 模拟SQL对文件进行增删改查

    #!/usr/bin/env python # _*_ coding:UTF-8 _*_ # __auth__: Dalhhin # Python 3.5.2,Pycharm 2016.3.2 # 2 ...

  5. [Leetcode Week4]H-Index

    H-Index题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/h-index/description/ Description Given an arr ...

  6. POJ1236 (强连通分量缩点求入度为0和出度为0的分量个数)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13804   Accepted: 55 ...

  7. 请求参数中的"+"号为什么会丢失,如何保证参数完整

    最近在开发中碰见一个问题,后端代码调用接口,在请求端参数没有任何问题,但是当接口接收到参数时,其中的加号全部变为了空格. 在查阅资料后发现是URLDecoder方法的问题,以下是URLDecoder的 ...

  8. ros下xtion用法

    xtion用openni2_launch openni2.launch就可以打开,但是在使用过程中有一些定制性问题: 首先弄清openni2_launch 中一些topic都是什么意思 http:// ...

  9. maven 通过 profile 设置多环境打包

    maven 在设计之初就考虑到了业务代码和测试代码的分开存放.将业务代码默认存放在  src/main  下,将测试代码放在  src/test  下,然后在各自目录下再细分  java  与 res ...

  10. 链表学习二:链表反转与查找倒数第K个

    //单链表反转 ListNode* RevertList(ListNode* m_pHead){ ListNode* pCurrent = m_pHead; ListNode* pPrev=NULL; ...