[问题] Android 的智能指标 sp

楼主: gn00618777 (非常念旧)   2016-04-24 23:18:17
不知道有没有人了解 Android 的 class sp?
在源码中我常看到下面用法
sp <Camera> c = new Camera(int)
new 在 c++中就是配置内存然后回传内存位址,但 c 又是一个物件。事实上,我根
本搞不清楚 android 的这个 c 代表的是什么。一开始我以为他是一个指标,去接new
出来的。但好像不是这么回事,因为我写了一个小程式去试
#include<iostream>
using namespace std;
template<typename T>
class sp{
public:
sp(T *other){
cout<<"This is the first constructor"<<endl;
}
sp(const sp<T>& other){}
};
class myClass{
public:
myClass():a(0){}
int getA(){
return a;
}
private:
int a;
};
int main(){
sp<myClass> c = new myClass();
cout<<c->sp()<<endl;
}
出来的讯息意思是说 c 是一个物件,不是一个指标~
[Error] base operand of '->' has non-pointer type 'sp<myClass>'
应该是 android 的 class sp 有写些动作.....,小弟底子差,看了几天也问了板友只
知道
sp<myClass> c = new myClass()
这个用法会用到 c++ 的 copy initialization,以及我知道他会 call 到 class sp 内
的 sp(T *other) 建构子, 我不知道这个 c 代表什么?
sp<myClass> c = new myClass() 这个式子
会动用到 class sp 内的哪一个 operater overload 还是 不会?
~"~
希望能从各位牛人汲取到一点建议
谢谢
作者: rexx   2016-04-24 23:56:00
kheresy.wordpress.com/2012/03/03/c11_smartpointer_p1/c是一个物件 但他有写operator->() 所以可以用pointer的写法c->xxx这样 感觉上直接当做pointer在用有兴趣可以去看他source codehttp://androidxref.com/6.0.1_r10/xref/system/core/include/utils/StrongPointer.h
作者: cha122977 (CHA)   2016-04-25 02:03:00
sp<Camera>可以直接当成Camera*来用用sp包他是为了自己管理delete时机(ARC)
楼主: gn00618777 (非常念旧)   2016-04-25 23:04:00
看到了一些关键,有时间消化完再来分享..

Links booklink

Contact Us: admin [ a t ] ucptt.com