Assigning shared_ptr to weak_ptr

℡╲_俬逩灬. 提交于 2019-12-02 12:49:45
Slava

Purpose of std::shared_ptr is to release managed object when last shared pointer which points to it is destroyed or reassigned to somewhere else. You created a temporary shared ptr, assgned it to std::weak_ptr and then it is just destroyed at the end of the expression. You need to keep it alive:

auto shared = std::shared_ptr<void>(operator new(60), [](void *pi) { operator delete(pi); });
std::weak_ptr<void> rw  = shared;

now rw would not expire at least while shared is still alive.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!