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

书上没讲,暂略

相关推荐
Vesan,3 分钟前
C++ static的使用方法及不同作用
java·jvm·c++
rqtz27 分钟前
【C++面向对象】封装(下):探索C++运算符重载设计精髓
开发语言·c++·运算符重载
小c君tt34 分钟前
qt/C++面试题自用学习(更新中)
c++·qt·学习·面试
愚润求学2 小时前
【专题刷题】双指针(三):两数之和,三数之和,四数之和
c++·笔记·leetcode·刷题
愚润求学2 小时前
【C++】map和set
开发语言·c++·笔记
末央&2 小时前
【C++】priority_queue的底层封装和实现
开发语言·c++
superior tigre2 小时前
C++学习:六个月从基础到就业——面向对象编程:虚函数与抽象类
开发语言·c++·学习
superior tigre3 小时前
C++学习:六个月从基础到就业——面向对象编程:重载运算符(下)
c++·学习
海棠蚀omo3 小时前
C++笔记-list
开发语言·c++·笔记
烟雨柳成烟4 小时前
C++学习Day0:c++简介
开发语言·c++·qt·学习