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 ...
随机推荐
- Nginx中的长连接
在nginx中,对于http1.0与http1.1是支持长连接的 我们知道,http请求是基于TCP协议之上的,那么,当客户端在发起请求前,需要先与服务端建立TCP连接,而每一次的TCP连接是需要三次 ...
- 如何让 linux unzip 命令 不输出结果
unzip xx.zip > /dev/null 2>&1 unzip xx.zip > /dev/null前半部分是将标准输出重定向到空设备, 后面的2>&1 ...
- 基于SSM框架web搜索功能的实现
这里适合选用于jsp搭建的网站,数据库采用MySQL 一.HTML <div class="header_search"> <input type="t ...
- 意想不到的chrome调试功能
没想到chrome的功能如此强大,绝非仅能使用console.log而已,碰到如此好文必定收藏,感谢大神,本文原创地址为:http://www.cnblogs.com/Wayou/p/chrome-c ...
- [Leetcode Week6]Best Time to Buy and Sell Stock
Best Time to Buy and Sell Stock 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/best-time-to-buy-and ...
- swift c++ oc 混编
http://www.tuicool.com/articles/QZNrErM iOS 里面 Swift与Objective-C混编,Swift与C++混编的一些比较 时间 2015-03-23 23 ...
- 【python】正则表达式相关
注意:Python3.X 的print要有括号, Python 2.x的不需要 放上学习时写的例子: import re m = re.match(r'(\w+) (\w+)(?P<sign&g ...
- docker从零开始(四)集群初体验,docker-machine swarm
介绍 在第三节中,选择了第二节中编写的应用程序,并通过将其转换为服务来定义它应如何在生产中运行,并生成五个应用实例 在本节中,将此应用程序部署到群集上,在多台计算机上运行它.多容器,多机应用程序通过连 ...
- Python 进阶 之 lambda 匿名函数
lambda 是个匿名函数,通常用于简单判断或者处理,例如判断一个数的奇偶性,过滤字符串,逻辑运算等等. lambda表达式: >>>lambda x:x*x >>> ...
- AC日记——Success Rate codeforces 807c
Success Rate 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> ...