Android 智能指针学习 一
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 智能指针学习 一的更多相关文章
- android 智能指针的学习先看邓凡平的书扫盲 再看前面两片博客提升
android 智能指针的学习先看邓凡平的书扫盲 再看前面两片博客提升
- Qt 智能指针学习(7种指针)
Qt 智能指针学习 转载自:http://blog.csdn.net/dbzhang800/article/details/6403285 从内存泄露开始? 很简单的入门程序,应该比较熟悉吧 ^_^ ...
- Android结构分析Android智能指针(两)
笔者:刘蒿羽 博客:http://blog.csdn.net/liuhaoyutz Android版本号:4.4.2 在上一篇文章中,我们分析了Android智能指针中的强指针sp,本文我们来分析弱指 ...
- Android智能指针SP WP使用方法介绍
Android手机操作系统既然是开源的操作系统.那么在具体的文件夹中就会存放着各种相关功能的开源代码.我们在使用的时候可以根据这些源代码进行相应的修改就能轻松的完成我们所需的功能.在这里大家就一起来看 ...
- Android智能指针sp wp详解
研究Android的时候,经常会遇到sp.wp的东西,网上一搜,原来是android封装了c++中对象回收机制.说明:1. 如果一个类想使用智能指针,那么必须满足下面两个条件: a. 该类是虚基 ...
- Qt 智能指针学习(7种QT的特有指针)
从内存泄露开始? 很简单的入门程序,应该比较熟悉吧 ^_^ #include <QApplication> #include <QLabel> int main(int arg ...
- Qt 智能指针学习(7种QT智能指针和4种std智能指针)
从内存泄露开始? 很简单的入门程序,应该比较熟悉吧 ^_^ #include <QApplication> #include <QLabel> int main(int arg ...
- Qt 智能指针学习
原地址:http://blog.csdn.net/dbzhang800/article/details/6403285 从内存泄露开始? 很简单的入门程序,应该比较熟悉吧 ^_^ #include & ...
- [转]Qt 智能指针学习
从内存泄露开始? 很简单的入门程序,应该比较熟悉吧 ^_^ #include <QApplication> #include <QLabel> int main(int arg ...
随机推荐
- kettle基础操作
ETL:抽取(extract).转换(transform).加载(load)至目的端的过程: Kettle是ETL工具代表之一,是pentaho中的一个数据整合的一个组件.Kettle里包括多个Job ...
- svn备份
公司的svn体量很大,要是一不小心误删了SVN版本库,就要哭了,所以有了下面的备份脚本 #每个版本库完全备份 #!/bin/bash SOUR_SVN="/var/www/svn" ...
- 51nod 1040 最大公约数之和
给出一个n,求1-n这n个数,同n的最大公约数的和.比如:n = 6 1,2,3,4,5,6 同6的最大公约数分别为1,2,3,2,1,6,加在一起 = 15 Input 1个数N(N <= ...
- GitLab 密码重设
内容全部来自: CSDN bisal GitLab 密码重设方法 假设注册邮箱为: abc@test.com 步骤 1) 登录 git 服务器 2) 执行: gitlab-rails console ...
- javascript批量输入表单
void((function(){ x=document.getElementsByTagName("a"); y = x[1] y.click() })()) void((fun ...
- linux软件管理(Vim编辑器使用) ——(七)
windows : .exe 安装 .卸载 安装: mysql.exe cc.exe 卸载 : 该软件唯一的标识 ,包名 alibaba android : *.apk 卸载 包 ...
- ssh保持连接
转载自: http://www.neatstudio.com/show-625-1.shtml http://www.linuxidc.com/Linux/2010-05/26031.htm (这一篇 ...
- requests上传文件
""" requests上传文件时,如果文件名是中文,会导致上传失败,参考:https://www.cnblogs.com/liaofeifight/p/5807901. ...
- LARGE_INTEGER类型
最近在研究Windows驱动程序开发,遇到这样一个语句: devExt->PortBase = (PUCHAR)(ULONG_PTR) portBasePA.QuadPart; 在源代码环境中 ...
- linux基础编程 套接字socket 完整的服务器端多线程socket程序【转】
转自:http://blog.csdn.net/ghostyu/article/details/7737203 此段程序来自我的一个项目中,稍微做了些修改,运行稳定,客户端程序比较简单所以未编写,可以 ...