c++11 智能指针 unique_ptr、shared_ptr与weak_ptr
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c++11 智能指针 unique_ptr、shared_ptr与weak_ptr,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3837字,纯文字阅读大概需要6分钟。
内容图文
c++11 智能指针 unique_ptr、shared_ptr与weak_ptr
C++11中有unique_ptr、shared_ptr与weak_ptr等智能指针(smart pointer),定义在<memory>中。
可以对动态资源进行管理,保证任何情况下,已构造的对象最终会销毁,即它的析构函数最终会被调用。
unique_ptr
unique_ptr持有对对象的独有权,同一时刻只能有一个unique_ptr指向给定对象(通过禁止拷贝语义、只有移动语义来实现)。
unique_ptr指针本身的生命周期:从unique_ptr指针创建时开始,直到离开作用域。
离开作用域时,若其指向对象,则将其所指对象销毁(默认使用delete操作符,用户可指定其他操作)。
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #include <memory> #include <vector> #include <map> void mytest() { std::unique_ptr<int> up1(newint(11)); // 无法复制的unique_ptr //unique_ptr<int> up2 = up1; // err, 不能通过编译 std::cout << *up1 << std::endl; // 11 std::unique_ptr<int> up3 = std::move(up1); // 现在p3是数据的唯一的unique_ptr std::cout << *up3 << std::endl; // 11 //std::cout << *up1 << std::endl; // err, 运行时错误 up3.reset(); // 显式释放内存 up1.reset(); // 不会导致运行时错误 //std::cout << *up3 << std::endl; // err, 运行时错误 std::unique_ptr<int> up4(newint(22)); // 无法复制的unique_ptr up4.reset(newint(44)); //"绑定"动态对象 std::cout << *up4 << std::endl; // 44 up4 = nullptr;//显式销毁所指对象,同时智能指针变为空指针。与up4.reset()等价 std::unique_ptr<int> up5(newint(55)); int *p = up5.release(); //只是释放控制权,不会释放内存 std::cout << *p << std::endl; //cout << *up5 << endl; // err, 运行时错误delete p; //释放堆区资源return; } int main() { mytest(); system("pause"); return0; }
shared_ptr
shared_ptr允许多个该智能指针共享第“拥有”同一堆分配对象的内存,这通过引用计数(reference counting)实现,会记录有多少个shared_ptr共同指向一个对象,一旦最后一个这样的指针被销毁,也就是一旦某个对象的引用计数变为0,这个对象会被自动删除。
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #include <memory> #include <vector> #include <map> void mytest() { std::shared_ptr<int> sp1(newint(22)); std::shared_ptr<int> sp2 = sp1; std::cout << "cout: " << sp2.use_count() << std::endl; // 打印引用计数 std::cout << *sp1 << std::endl; std::cout << *sp2 << std::endl; sp1.reset(); // 显示让引用计数减一 std::cout << "count: " << sp2.use_count() << std::endl; // 打印引用计数 std::cout << *sp2 << std::endl; // 22return; } int main() { mytest(); system("pause"); return0; }
weak_ptr
weak_ptr是为配合shared_ptr而引入的一种智能指针来协助shared_ptr工作,它可以从一个shared_ptr或另一个weak_ptr对象构造,它的构造和析构不会引起引用计数的增加或减少。没有重载 * 和 -> 但可以使用lock获得一个可用的shared_ptr对象
weak_ptr的使用更为复杂一点,它可以指向shared_ptr指针指向的对象内存,却并不拥有该内存,而使用weak_ptr成员lock,则可返回其指向内存的一个share_ptr对象,且在所指对象内存已经无效时,返回指针空值nullptr。
注意:weak_ptr并不拥有资源的所有权,所以不能直接使用资源。
可以从一个weak_ptr构造一个shared_ptr以取得共享资源的所有权。
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #include <memory> #include <vector> #include <map> void check(std::weak_ptr<int> &wp) { std::shared_ptr<int> sp = wp.lock(); // 转换为shared_ptr<int>if (sp != nullptr) { std::cout << "still: " << *sp << std::endl; } else { std::cout << "still: " << "pointer is invalid" << std::endl; } } void mytest() { std::shared_ptr<int> sp1(newint(22)); std::shared_ptr<int> sp2 = sp1; std::weak_ptr<int> wp = sp1; // 指向shared_ptr<int>所指对象 std::cout << "count: " << wp.use_count() << std::endl; // count: 2 std::cout << *sp1 << std::endl; // 22 std::cout << *sp2 << std::endl; // 22 check(wp); // still: 22 sp1.reset(); std::cout << "count: " << wp.use_count() << std::endl; // count: 1 std::cout << *sp2 << std::endl; // 22 check(wp); // still: 22 sp2.reset(); std::cout << "count: " << wp.use_count() << std::endl; // count: 0 check(wp); // still: pointer is invalidreturn; } int main() { mytest(); system("pause"); return0; }
原文:http://www.cnblogs.com/lsgxeva/p/7788061.html
内容总结
以上是互联网集市为您收集整理的c++11 智能指针 unique_ptr、shared_ptr与weak_ptr全部内容,希望文章能够帮你解决c++11 智能指针 unique_ptr、shared_ptr与weak_ptr所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。