博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
weak_ptr使用的几个实例
阅读量:4092 次
发布时间:2019-05-25

本文共 4978 字,大约阅读时间需要 16 分钟。

#include 
#include
#include
#include
#include
#include
///// reference: http://en.cppreference.com/w/cpp/memory/weak_ptrstd::weak_ptr
gw;void f(){ if (auto spt = gw.lock()) // Has to be copied into a shared_ptr before usage { std::cout << *spt << "\n"; } else { std::cout << "gw is expired\n"; }}int main1(){ { auto sp = std::make_shared
(42); gw = sp; f(); } f(); return 0;}/// reference: http://stackoverflow.com/questions/12030650/when-is-stdweak-ptr-usefulint main3(){ // OLD, problem with dangling pointer // PROBLEM: ref will point to undefined data! int *ptr = new int(10); int *ref = ptr; delete ptr; // NEW // SOLUTION: check expired() or lock() to determine if pointer is valid // empty definition std::shared_ptr
sptr; // takes ownership of pointer sptr.reset(new int); *sptr = 10; std::cout << "use count test 1:" << sptr.use_count() << '\n'; // get pointer to data without taking ownership std::weak_ptr
weak1 = sptr; std::cout << "use count test 2:" << sptr.use_count() << '\n'; // deletes managed object, acquires new pointer std::shared_ptr
sptr_bck = sptr; std::cout << "use count test 3:" << sptr.use_count() << '\n'; std::cout << "use count test 3.5:" << sptr_bck.use_count() << '\n'; //sptr.reset(new int); std::cout << "use count test 4:" << sptr.use_count() << '\n'; *sptr = 5; // get pointer to new data without taking ownership std::weak_ptr
weak2 = sptr; std::weak_ptr
weak3 = weak2; std::cout << "use count test 5:" << sptr.use_count() << '\n'; // weak1 is expired! if (auto tmp = weak1.lock()) std::cout << *tmp << '\n'; else std::cout << "weak1 is expired\n"; // weak2 points to new data (5) if (auto tmp = weak2.lock()) std::cout << *tmp << '\n'; else std::cout << "weak2 is expired\n"; return 0;}//// reference: https://msdn.microsoft.com/en-us/library/hh279672.aspxclass Controller{ public: int Num; std::string Status; std::vector
> others; explicit Controller(int i) : Num(i), Status("On") { std::cout << "Creating Controller" << Num << std::endl; } ~Controller() { std::cout << "Destroying Controller" << Num << std::endl; } // Demonstrates how to test whether the pointed-to memory still exists or not. void CheckStatuses() const { for_each(others.begin(), others.end(), [](std::weak_ptr
wp) { try { auto p = wp.lock(); std::cout << "Status of " << p->Num << " = " << p->Status << std::endl; } catch (std::bad_weak_ptr b) { std::cout << "Null object" << std::endl; } }); }};void RunTest(){ std::vector
> v; v.push_back(std::shared_ptr
(new Controller(0))); v.push_back(std::shared_ptr
(new Controller(1))); v.push_back(std::shared_ptr
(new Controller(2))); v.push_back(std::shared_ptr
(new Controller(3))); v.push_back(std::shared_ptr
(new Controller(4))); // Each controller depends on all others not being deleted. // Give each controller a pointer to all the others. for (int i = 0; i < v.size(); ++i) { for_each(v.begin(), v.end(), [v, i](std::shared_ptr
p) { if (p->Num != i) { v[i]->others.push_back(std::weak_ptr
(p)); std::cout << "push_back to v[" << i << "]: " << p->Num << std::endl; } }); } for_each(v.begin(), v.end(), [](std::shared_ptr
&p) { std::cout << "use_count = " << p.use_count() << std::endl; p->CheckStatuses(); });}int main4(){ RunTest(); std::cout << "Press any key" << std::endl; char ch; std::cin.getline(&ch, 1); return 0;}// reference: https://oopscenities.net/2014/08/03/c-smart-pointers-part-5-weak_ptr/struct Child;struct Parent{ std::shared_ptr
child; ~Parent() { std::cout << "Bye Parent" << std::endl; } void hi() const { std::cout << "Hello" << std::endl; }};struct Child{ //std::weak_ptr
parent; std::shared_ptr
parent; // memory leak ~Child() { std::cout << "Bye Child" << std::endl; }};int main6(){ auto parent = std::make_shared
(); auto child = std::make_shared
(); parent->child = child; child->parent = parent; //child->parent.lock()->hi(); child->parent->hi(); return 0;}/// reference: http://thispointer.com/shared_ptr-binary-trees-and-the-problem-of-cyclic-references/class Node{ int value;public: std::shared_ptr
leftPtr; std::shared_ptr
rightPtr; // Just Changed the shared_ptr to weak_ptr std::weak_ptr
parentPtr; Node(int val) : value(val) { std::cout << "Contructor" << std::endl; } ~Node() { std::cout << "Destructor" << std::endl; }};int main(){ std::shared_ptr
ptr = std::make_shared
(4); ptr->leftPtr = std::make_shared
(2); ptr->leftPtr->parentPtr = ptr; ptr->rightPtr = std::make_shared
(5); ptr->rightPtr->parentPtr = ptr; std::cout << "ptr reference count = " << ptr.use_count() << std::endl; std::cout << "ptr->leftPtr reference count = " << ptr->leftPtr.use_count() << std::endl; std::cout << "ptr->rightPtr reference count = " << ptr->rightPtr.use_count() << std::endl; std::cout << "ptr->rightPtr->parentPtr reference count = " << ptr->rightPtr->parentPtr.lock().use_count() << std::endl; std::cout << "ptr->leftPtr->parentPtr reference count = " << ptr->leftPtr->parentPtr.lock().use_count() << std::endl; return 0;}

转载地址:http://frcii.baihongyu.com/

你可能感兴趣的文章
极客日报:日本的首颗 5nm 芯片公布;虾米音乐正式关停;网易云音乐再次喊话酷狗...
查看>>
影响科学圈的那些计算机代码
查看>>
乐视视频 App 图标改为“欠 122 亿”,网友:我在别家分红包,却在你家随份子!...
查看>>
【回顾】2020年打工人的100个心酸瞬间
查看>>
乔布斯18岁求职信拍卖价22.24万美元,值吗?
查看>>
为何程序员总喜欢写技术博客,看完恍然大悟...
查看>>
GitHub 上有哪些 Java 面试 / 学习相关的仓库推荐?
查看>>
推荐一位机器学习大佬,手握 28000 Star,GitHub 全球排名 120!
查看>>
盘点下阿里开源的 29 个开源项目
查看>>
学编程必备的 10 大技术公众号!
查看>>
实用!GitHub 终于支持 multi-line comments!
查看>>
盘点那些走向世界的中国开源项目
查看>>
9 月份 GitHub 上出现了哪些热门开源项目?
查看>>
团队开发中 Git 最佳实践,不给队友拖后腿!
查看>>
从 6 万用户评论中,选出 23 门全世界最好的 CS 免费课!
查看>>
Vue 3 源码公布
查看>>
被百度 AI 官方认证的大学,到底是什么水平?
查看>>
近 50 年来最具影响力的 10 种编程语言,都是谁发明的?
查看>>
该如何弥补 GitHub 功能缺陷?
查看>>
GitHub 标星 5w+,计算机小白到大牛的学习之路!
查看>>