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

书上没讲,暂略

相关推荐
xcyxiner2 小时前
snmp wireshark 抓包
c++
rocksun2 小时前
BJARNE STROUSTRUP谈C++的演变
c++
我不是代码教父2 小时前
[原创](Modern C++)现代C++的关键性概念: 非常独特的std::sentinel_for概念(哨兵概念)
开发语言·c++·sentinel
JuicyActiveGilbert2 小时前
【C++设计模式】第十六篇:迭代器模式(Iterator)
c++·设计模式·迭代器模式
commonbelive2 小时前
考研机试常见基本题型
c语言·c++·算法
希望_睿智2 小时前
C++网络编程之套接字选项配置
c++·网络协议
情深不寿3173 小时前
数据结构--AVL树
数据结构·c++
LuckyRich13 小时前
【高并发内存池】释放内存 + 申请和释放总结
开发语言·c++·缓存
明月看潮生3 小时前
青少年编程与数学 02-010 C++程序设计基础 29课题、继承
开发语言·c++·青少年编程·编程与数学
lucky1_1star4 小时前
FX-C++结构体与类的区别
开发语言·c++