C++11:智能指针

文章目录

头文件memory

名称空间 std

what is 智能指针?

诸如 auto_ptr 以及 C++11 新增的 shared_ptr 和 unique_ptr 等智能指针模板使得管理由 new 分配的内存更容易。它们是类。如果使用这些智能指针(而不是常规指针)来保存 new 返回的地址,则不必在以后使用删除运算符。智能指针对象过期时(即超出了类的作用域时),其析构函数将自动调用 delete 运算符,无需手动释放new出来的内存。

四种智能指针

先看下面这段程序:

cpp 复制代码
xxx_ptr<string> pl(new string("auto");
xxx_ptr<string> p2;
p2=p1; 

其中xxx_ptr仅仅是指向string对象的指针。如果不对赋值运算符进行重载,那么在程序结束时释放p1p2一定会异常,因为默认浅拷贝。对于赋值运算符的重载,有以下几种实现办法:

  1. 深拷贝(我们之前自己实现的时候用这个)
  2. 建立所有权概念(auto_ptr, unique_ptr)
  3. 增加指针计数器 (shared_ptr)

使用方法

cpp 复制代码
// 1. 使用构造函数

auto_ptr<string> p1(new string("hahahaha")); 
auto_ptr<string> p2(p_str); //p_str是普通指针
unique_ptr<string> p3(new string("hahahaha")); 
unique_ptr<string> p4(p_str); 
shared_ptr<string> p5(new string("hahahaha")); 
shared_ptr<string> p6(p_str);

//2. 使用make_shared函数

shared_ptr<string> p7 = make_shared<string>("hahahaha")
int tmp = p7.usecount() // 引用次数
auto_ptr

是C++98的方案,现已弃用。

cpp 复制代码
auto_ptr<string> pl(new string("auto");
auto_ptr<string> p2;
p2=p1; //这句话使得p1指向的位置无效,所有权完全给到p2.这句话在编译时不会有任何问题,执行时,再次访问p1内容时却会使程序异常。这是"不安全"的现象,所以弃用
unique_ptr

相比之下,unique_ptr:

cpp 复制代码
unique_ptr<string> pl(new string("auto");
unique_ptr<string> p2;
p2=p1; //编译不通过。被认为是比auto_ptr更"安全"。

那是不是unique_ptr完全不能通过赋值得来呢?如果赋值号右侧的是临时右值的话,是可以的。

cpp 复制代码
unique_ptr<string>demo{const char* s)
{
    unique ptr<string> temp(new string(s));
    return temp;
}
int main()
{ 
    unique_ptr<string> ps;
    ps = demo("Uniquely special");
    return 0;
}

那unique_ptr 如何能够区分安全和不安全的用法呢?答案是它使用了C++11新增的移动构造函数和右值引用。

可以使用new[],而另外两个不能使用new[]

shared_ptr

策略:创建智能更高的指针,跟踪引用特定对象的智能指针数。这称为引用计数(reference counting)。

例如,赋值时,让数将加1,而指针过期时,计数将减1.仅当最后一个指针过期时,才调用delete。

weak_ptr

书上没讲,暂略

相关推荐
阿沁QWQ21 分钟前
单例模式的两种设计
开发语言·c++·单例模式
六bring个六28 分钟前
qtcreater配置opencv
c++·qt·opencv·计算机视觉·图形渲染·opengl
qwertyuiop_i33 分钟前
pe文件二进制解析(用c/c++解析一个二进制pe文件)
c语言·c++·pe文件
yxc_inspire1 小时前
基于Qt的app开发第八天
开发语言·c++·qt
June`2 小时前
专题三:穷举vs暴搜vs深搜vs回溯vs剪枝(全排列)决策树与递归实现详解
c++·算法·深度优先·剪枝
我叫珂蛋儿吖2 小时前
[redis进阶六]详解redis作为缓存&&分布式锁
运维·c语言·数据库·c++·redis·分布式·缓存
yxc_inspire3 小时前
基于Qt的app开发第七天
开发语言·c++·qt·app
周Echo周3 小时前
20、map和set、unordered_map、un_ordered_set的复现
c语言·开发语言·数据结构·c++·算法·leetcode·list
☆平常心☆4 小时前
UE5通过C++实现TcpSocket连接
c++·ue5
dot to one5 小时前
Qt 中 QWidget涉及的常用核心属性介绍
开发语言·c++·qt