理解smart pointer之三:unique_ptr
unique_ptr最先在boost中被定义,后来被C++标准委员会选中为C++11的feature之一。
std::unique_ptr is a smart pointer that retains sole ownership of an object through a pointer and destroys that object when the unique_ptr goes out of scope. No two unique_ptr instances can manage the same object.
The object is destroyed and its memory deallocated when either of the following happens:
- unique_ptr managing the object is destroyed
- unique_ptr managing the object is assigned another pointer via operator= or reset().
The object is destroyed using a potentially user-supplied deleter by calling Deleter(ptr). The deleter calls the destructor of the object and dispenses the memory.
A unique_ptr may also own no objects, in which case it is called empty.
There are two versions of std::unique_ptr:
- Manages the lifetime of a single object (e.g. allocated with new)
- Manages the lifetime of a dynamically-allocated array of objects (e.g. allocated with new[])
The class satisfies the requirements of MoveConstructible and MoveAssignable, but not the requirements of either CopyConstructible or CopyAssignable. [see C++11 Concepts]
理解smart pointer之三:unique_ptr的更多相关文章
- smart pointer
smart pointer是一种abstract data type,它可以模仿指针的行为,而且额外提供了一系列诸如自己主动内存管理.边界检查等特性,这些特性是为了在保证效率的基础上降低因为对指针的不 ...
- C++2.0新特性(八)——<Smart Pointer(智能指针)之unique_ptr>
一.概念介绍 unique_ptr它是一种在异常发生时可帮助避免资源泄露的smart pointer,实现了独占式拥有的概念,意味着它可确保一个对象和其他相应资源在同一时间只被一个pointer拥有, ...
- c++(smart pointer)
(一)首先对智能指针有一些概念性的了解 **********本部分内容摘自开源中国社区http://my.oschina.net/u/158589/blog/28994******** 1.什么是智能 ...
- Smart Pointer Guidelines
For Developers > Smart Pointer Guidelines What are smart pointers? Smart pointers are a specif ...
- C++2.0新特性(六)——<Smart Pointer(智能指针)之shared_ptr>
Smart Pointer(智能指针)指的是一类指针,并不是单一某一个指针,它能知道自己被引用的个数以至于在最后一个引用消失时销毁它指向的对象,本文主要介绍C++2.0提供的新东西 一.Smart P ...
- [CareerCup] 13.8 Smart Pointer 智能指针
13.8 Write a smart pointer class. A smart pointer is a data type, usually implemented with templates ...
- Why do we need smart pointer and how to implement it.
Here are two simple questions. Problem A #include <string> include <iostream> using name ...
- c++ smart pointer
智能指针(smart pointer)是存储指向动态分配(堆)对象指针的类,用于生存期控制,能够确保自动正确的销毁动态分配的对象,防止内存泄露.它的一种通用实现技术是使用引用计数(reference ...
- 深入理解C指针之三:指针和函数
原文:深入理解C指针之三:指针和函数 理解函数和指针的结合使用,需要理解程序栈.大部分现代的块结构语言,比如C,都用到了程序栈来支持函数的运行.调用函数时,会创建函数的栈帧并将其推到程序栈上.函数返回 ...
随机推荐
- Material Design Button 样式
132down voteaccepted I will add my answer since I don't use any of the other answers provided. With ...
- PS去掉图片上的文字的6种基本方法,动态教程
1.使用仿制图章工具去除文字这是比较常用的方法.具体的操作是,选取仿制图章工具,按住Alt键,在无文字区域点击相似的色彩或图案采样,然后在文字区域拖动鼠标复制以复盖文字.要注意的是,采样点即为复制的起 ...
- 将cantk runtime嵌入到现有的APP中
1,先取cantk-runtime-demos到本地: git clone https://github.com/drawapp8/cantk-runtime-demos 2,创建一个Android ...
- linux笔记:linux常用命令-用户管理命令
用户管理命令:useradd(添加用户) 用户管理命令:passwd(设置和修改用户密码) 用户管理命令:who(查看所有登录用户的信息)
- linux笔记:用户配置文件
用户信息文件:/etc/passwd功能:存放用户基本信息每一行代表一个用户. 第一字段:用户名称第二字段:密码标志(只是一个密码的占位符,表示该用户有密码,不存放真正的密码)第三字段:UID(用户I ...
- 能源项目xml文件标签释义--DefaultAdvisorAutoProxyCreator
[Spring]AOP拦截-三种方式实现自动代理 这里的自动代理,我讲的是自动代理bean对象,其实就是在xml中让我们不用配置代理工厂,也就是不用配置class为org.springframewor ...
- DSP EPWM学习笔记2 - EPWM相关寄存器设置问题解析
DSP EPWM学习笔记2 - EPWM相关寄存器设置问题解析 彭会锋 本篇主要针对不太熟悉的TZ 故障捕获 和 DB 死区产生两个子模块进行学习研究 感觉TI的寄存器命名还是有一定规律可循的 SEL ...
- 初学java之常用组件
import javax.swing.*; import java.awt.*; class Win extends JFrame { JTextField mytext; // 设置一个文本区 JB ...
- Simplify Path [LeetCode]
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- jquery表单验证使用插件formValidator
JQuery表单验证使用插件formValidator 作者: 字体:[增加 减小] 类型:转载 时间:2012-11-10我要评论 jquery表单验证使用插件formValidator,可供有需求 ...