草庐IT

weak_ptr

全部标签

c++ - 在 std::move() 之后 unique_ptr 会发生什么?

这段代码就是我想做的:Tony&Movie::addTony(){Tony*newTony=newTony;std::unique_ptrtony(newTony);attachActor(std::move(tony));return*newTony;}我想知道我是否可以这样做:Tony&Movie::addTony(){std::unique_ptrtony(newTony);attachActor(std::move(tony));return*tony.get();}但是*tony.get()会是同一个指针还是null?我知道我可以验证,但它的标准做法是什么?

c++ - 在 std::move() 之后 unique_ptr 会发生什么?

这段代码就是我想做的:Tony&Movie::addTony(){Tony*newTony=newTony;std::unique_ptrtony(newTony);attachActor(std::move(tony));return*newTony;}我想知道我是否可以这样做:Tony&Movie::addTony(){std::unique_ptrtony(newTony);attachActor(std::move(tony));return*tony.get();}但是*tony.get()会是同一个指针还是null?我知道我可以验证,但它的标准做法是什么?

c++ - 为什么 shared_ptr<T>::use_count() 返回 long 而不是 unsigned 类型?

shared_ptr观察者20.8.2.2.5C++14最终草案(n4296)longuse_count()constnoexcept;Returns:thenumberofshared_ptrobjects,*thisincluded,thatshareownershipwith*this,or0when*thisisempty.[Note:use_count()isnotnecessarilyefficient.—endnote] 最佳答案 根据这个页面http://www.open-std.org/jtc1/sc22/wg21

c++ - 为什么 shared_ptr<T>::use_count() 返回 long 而不是 unsigned 类型?

shared_ptr观察者20.8.2.2.5C++14最终草案(n4296)longuse_count()constnoexcept;Returns:thenumberofshared_ptrobjects,*thisincluded,thatshareownershipwith*this,or0when*thisisempty.[Note:use_count()isnotnecessarilyefficient.—endnote] 最佳答案 根据这个页面http://www.open-std.org/jtc1/sc22/wg21

python - "weakly-referenced object no longer exists"是什么意思?

我正在运行Python代码并收到以下错误消息:Exceptionexceptions.ReferenceError:'weakly-referencedobjectnolongerexists'in>ignored有人知道这是什么意思吗?附:这是产生错误的代码:importsqliteclasscrawler:def__init__(self,dbname):tmp=sqlite.connect(dbname)self.con=tmp.cursor()def__del__(self):self.con.close()crawler=crawler('searchindex.db')

python - "weakly-referenced object no longer exists"是什么意思?

我正在运行Python代码并收到以下错误消息:Exceptionexceptions.ReferenceError:'weakly-referencedobjectnolongerexists'in>ignored有人知道这是什么意思吗?附:这是产生错误的代码:importsqliteclasscrawler:def__init__(self,dbname):tmp=sqlite.connect(dbname)self.con=tmp.cursor()def__del__(self):self.con.close()crawler=crawler('searchindex.db')

java - Android Asynctask : Use weak reference for context to avoid device rotate screen

在ApressProAndroid4作者说过:[...]contextofcurrentlyrunningactivitywillnolongerbevalidwhenthedeviceisrotated.[...]Oneapproachistouseaweakreferencetotheactivityinsteadofahardreference[...]但作者只是建议这样做,并没有说明它是如何完成的。有谁做过,请举个例子。 最佳答案 在你的AsyncTask的某个地方,你会想要传递你的Activity。然后,您将该引用保存在弱

java - Android Asynctask : Use weak reference for context to avoid device rotate screen

在ApressProAndroid4作者说过:[...]contextofcurrentlyrunningactivitywillnolongerbevalidwhenthedeviceisrotated.[...]Oneapproachistouseaweakreferencetotheactivityinsteadofahardreference[...]但作者只是建议这样做,并没有说明它是如何完成的。有谁做过,请举个例子。 最佳答案 在你的AsyncTask的某个地方,你会想要传递你的Activity。然后,您将该引用保存在弱

C++智能指针shared_ptr用法

目录shared_ptr功能介绍shared_ptr提供的接口shared_ptr初始化shared_ptr管理指针的构造和析构shared_ptr获取原始指针shared_ptr的线程安全shared_ptr应用之enable_shared_from_this写在前面的总结:一个shared_ptr对象管理一个指针(newT,在堆空间),多个shared_ptr对象可以管理同一个指针,只有某个shared_ptr对象第一次初始化指针时才执行指针的构造函数,管理同一个指针的shared_ptr对象个数称为引用计数,这个引用计数保存在每个管理该指针的shared_ptr对象中,当引用计数为0时,

c++ - 调用函数时,C++ 中的 ptr** 和 ptr*& 之间有区别或首选吗?

在函数中更改指针是否有区别或首选方法?以这个片段为例voidchange(int**ptr){**ptr=50;*ptr=nullptr;}voidchange(int*&ptr){*ptr=50;ptr=nullptr;}intmain(){inta=5;int*ptr=&a;int**ptr2=&ptr;std::cout似乎这两个更改函数都可以实现我的要求,但我不确定它们之间的区别,除了引用函数不会创建指针的拷贝外? 最佳答案 你可以有一个空指针,但不能有一个空引用。您可以为第一个提供nullptr,它将编译1,因为存在从st