std::shared_ptr(二)
|
Defined in header <memory>
|
||
|
template< class T > class shared_ptr;
|
(since C++11) | |
std::shared_ptr is a smart pointer that retains shared ownership of an object through a pointer. Severalshared_ptr objects may own the same object; the object is destroyed when the last remaining shared_ptr pointing to it is destroyed or reset. The object is destroyed using delete-expression or a custom deleter that is supplied toshared_ptr during construction.
A shared_ptr may also own no objects, in which case it is called empty.
shared_ptr meets the requirements of CopyConstructible and CopyAssignable.
Member types
| Member type | Definition |
| element_type | T |
Member functions
constructs new shared_ptr (public member function) |
|
destructs the owned object if no more shared_ptrs link to it (public member function) |
|
assigns the shared_ptr (public member function) |
|
Modifiers |
|
| replaces the managed object (public member function) |
|
| swaps the managed objects (public member function) |
|
Observers |
|
| returns a pointer to the managed object (public member function) |
|
| dereferences pointer to the managed object (public member function) |
|
returns the number of shared_ptr objects referring to the same managed object (public member function) |
|
checks whether the managed object is managed only by the current shared_ptr instance (public member function) |
|
| checks if there is associated managed object (public member function) |
|
| provides owner-based ordering of shared pointers (public member function) |
|
Non-member functions
| creates a shared pointer that manages a new object (function template) |
|
| creates a shared pointer that manages a new object allocated using an allocator (function template) |
|
| applies static_cast, dynamic_cast or const_cast to the type of the managed object (function template) |
|
| returns the deleter of specified type, if owned (function template) |
|
compares with another shared_ptr or with nullptr (function template) |
|
| outputs the value of the managed pointer to an output stream (function template) |
|
|
(C++11)
|
specializes the std::swap algorithm (function template) |
| specializes atomic operations (function template) |
|
Helper classes
|
(C++11)
|
hash support for std::shared_ptr (class template specialization) |
Implementation notes
In a typical implementation, std::shared_ptr holds only two pointers:
- a pointer to the referenced object
- a pointer to control block
Where the control block is a dynamically-allocated object that holds:
- a pointer to the managed object or the managed object itself
- the deleter
- the allocator
- the number of
shared_ptrs that own the managed object - the number of
weak_ptrs that refer to the managed object
When shared_ptr is created by calling std::make_shared or std::allocate_shared, the control block holds the managed object directly, as a data member. When shared_ptr is created by calling a constructor, a pointer is stored.
The pointer held by the shared_ptr directly is the one returned by get(), while the pointer/object held by the control block is the one that will be deleted when the number of shared owners reaches zero: these pointers are not necessarily equal.
The destructor of shared_ptr decrements the number of shared owners of the control block, and if that reaches zero, the control block calls the destructor of the managed object, but the control block does not deallocate itself until thestd::weak_ptr counter reaches zero as well.
这个360还不让人复制,直接把JS禁掉,以前学的不是白学的
std::shared_ptr(二)的更多相关文章
- tolua 转换 std::shared_ptr
tolua 转换 std::shared_ptr 自从c++11以后std::shared_ptr几乎是比用的东西,经常会遇到类似如下应用 std::shared_ptr<Tst_ShareTe ...
- std::shared_ptr
在std::shared_ptr被引入之前,C++标准库中实现的用于管理资源的智能指针只有std::auto_ptr一个而已.std::auto_ptr的作用非常有限,因为它存在被管理资源的所有权转移 ...
- (译+原)std::shared_ptr及std::unique_ptr使用数组
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5462363.html 参考网址: http://stackoverflow.com/questions ...
- std::shared_ptr<void>的工作原理
前戏 先抛出两个问题 如果delete一个指针,但是它真实的类型和指针类型不一样会发生什么? 是谁调用了析构函数? 下面这段代码会发生什么有趣的事情? // delete_diff_type.cpp ...
- 巧用std::shared_ptr全局对象释放单例内存
巧用std::shared_ptr 单例的使用相对比较广泛,但是需要在程序退出前调用它的析构函数对数据进行释放,常规做法是在main函数末尾进行释放工作, 但是这样相对比较繁琐,因此便有了利用全局变量 ...
- Item 20: 使用std::weak_ptr替换会造成指针悬挂的类std::shared_ptr指针
本文翻译自modern effective C++,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 矛盾的是,我们很容易就能创造出一个和std::shared_ptr ...
- std::shared_ptr 和普通指针的转换
相互转化见示例 struct test { int num; string name; }; test* pTest = new test(); std::shared_ptr<test> ...
- C++11 std::shared_ptr总结与使用
最近看代码,智能指针用的比较多,自己平时用的少,周末自己总结总结.方便后续使用. std::shared_ptr大概总结有以下几点: (1) 智能指针主要的用途就是方便资源的管理,自动释放没有指针引用 ...
- std::shared_ptr之deleter的巧妙应用
本文由作者邹启文授权网易云社区发布. std::shared_ptr 一次创建,多处共享,通过引用计数控制生命周期. 实例 在邮箱大师PC版中,我们在实现搜索时,大致思路是这样的: 每一个账号都有一个 ...
随机推荐
- Java中的Random()函数
今天在做Java练习的时候注意到了Java里面的一个随机函数——Random,刚开始只是知道这个函数具有随机取值的作用,于是上网搜索了资料一番,做了一下一些关于Random函数的总结: Java中 ...
- Install MySQL on Mac OS X——MAC安装MySQL
很多关于如何安装MySQL的教程已经过时了,或者比必须的步骤复杂得多.这篇教程将展示如何安装MySQL,启动MySQL,以root用户进入MySQL,以及创建删除退出数据库. Step 1: 下载My ...
- 从Python爬虫到SAE云和微信公众号:二、新浪SAE上搭建微信服务
目的:用PHP在SAE上搭建一个微信公众号的服务器. 1.申请一个SAE云账号 SAE申请地址:http://sae.sina.com.cn/ 可以使用微博账号登陆,SAE是新浪的云服务,时间也比较 ...
- Google java代码风格导入Eclipse
Git地址 https://github.com/codeset/google-java-styleguide 下载配置文件在Eclipse中执行导入:Window -> Preferences ...
- (转)shell实例手册
原文地址:http://hi.baidu.com/quanzhou722/item/f4a4f3c9eb37f02d46d5c0d9 实在是太好的资料了,不得不转 shell实例手册 0说明{ 手册制 ...
- Web API 基于ASP.NET Identity的Basic Authentication
今天给大家分享在Web API下,如何利用ASP.NET Identity实现基本认证(Basic Authentication),在博客园子搜索了一圈Web API的基本认证,基本都是做的Forms ...
- http长链接与短链接
http://www.cnblogs.com/cswuyg/p/3653263.html keep-live模式 这个博客写的很全:http://www.cnblogs.com/skynet/arch ...
- commonjs amd cmd的区别
一篇博客告诉你三者的区别:http://zccst.iteye.com/blog/2215317 告诉你三者同requirejs seajs的区别:http://blog.chinaunix.net/ ...
- [USACO2003][poj2018]Best Cow Fences(数形结合+单调队列维护)
http://poj.org/problem?id=2018 此乃神题……详见04年集训队论文周源的,看了这个对斜率优化dp的理解也会好些. 分析: 我们要求的是{S[j]-s[i-1]}/{j-(i ...
- [Aaronyang] 写给自己的WPF4.5 失传的第十本秘籍4[wpf使用FontAwesome,并送其他3招心法]
总有一个人他教会你成长,然后又独自离开--Aaronyang的博客(www.ayjs.net)-www.8mi.me =============时隔两年后再看WPF,有些秘籍不太适合公开,公开了就不值 ...