zoukankan      html  css  js  c++  java
  • C++11 智能指针

    【1】C++11智能指针

    (1)废弃auto_ptr

    auto_ptr应用示例:

    1 auto_ptr<string> p1(new string("auto_ptr"));
    2 auto_ptr<string> p2;
    3 p2 = p1; // auto_ptr不会报错

    此语句 p2 = p1; 编译时不会报错,但p2剥夺了p1的所有权,当程序运行时访问p1将会报错。

    所以auto_ptr的缺点:存在潜在的崩溃问题。

    (2)unique_ptr、shared_ptr、weak_ptr

    应用示例如下:

     1 #include <iostream>
     2 #include <memory>
     3 using namespace std;
     4 
     5 void test_unique_ptr()
     6 {
     7     unique_ptr<int> up1(new int(2020));  // 无法复制的unique_ptr
     8//   unique_ptr<int> up2 = up1;           // 不能通过编译
     9     cout << (*up1) << endl;              // 2020
    10     unique_ptr<int> up3 = move(up1);     // 现在up3是数据唯一的unique_ptr智能指针
    11     cout << (*up3) << endl;              // 2020
    12//   cout << (*up1) << endl;              // 运行时错误
    13     up3.reset();                         // 显式释放内存
    14     up1.reset();                         // 不会导致运行时错误
    15//   cout << (*up3) << endl;              // 运行时错误
    16 }
    17 
    18 void test_shared_ptr()
    19 {
    20     shared_ptr<int> sp1(new int(131));
    21     shared_ptr<int> sp2 = sp1;
    22     cout << (*sp1) << endl;  // 131
    23     cout << (*sp2) << endl;  // 131
    24     sp1.reset();
    25//   cout << (*sp1) << endl;
    26     cout << (*sp2) << endl;  // 131
    27 }
    28 
    29 void check(weak_ptr<int> & wp)
    30 {
    31     shared_ptr<int> sp = wp.lock(); // 转换为shared_ptr
    32     if (sp != nullptr)
    33     {
    34         cout << "still " << (*sp) << endl;
    35     }
    36     else
    37     {
    38         cout << "pointer is invalid." << endl;
    39     }
    40 
    41 }
    42 void test_weak_ptr()
    43 {
    44     shared_ptr<int> sp1(new int(2020));
    45     shared_ptr<int> sp2 = sp1;
    46     weak_ptr<int> wp = sp1;  // 指向shared_ptr<int>所指对象
    47     cout << (*sp1) << endl;  // 2020
    48     cout << (*sp2) << endl;  // 2020
    49 
    50     check(wp);               // still 2020
    51     sp1.reset();
    52     cout << (*sp2) << endl;  // 2020
    53     check(wp);               // still 2020
    54     sp2.reset();
    55     check(wp);               // pointer is invalid
    56 }
    57 
    58 int main()
    59 {
    60     test_unique_ptr();
    61     test_shared_ptr();
    62     test_weak_ptr();
    63 }

    [1] unique_ptr实现独占式拥有概念,保证同一时间内只有一个智能指针可以指向该对象。

    [2] shared_ptr实现共享式拥有概念。多个智能指针可以指向相同对象,利用引用计数实现,该对象和其相关资源会在“最后一个引用被销毁”时候释放。

    [3] weak_ptr是一种不控制对象生命周期的智能指针,指向一个shared_ptr管理的对象,进行该对象内存管理的是那个强引用的shared_ptr。

    weak_ptr只是提供了对管理对象的一个访问手段,为了配合shared_ptr而引入的一种智能指针,主要用来协助 shared_ptr 工作。

    weak_ptr只可以从一个 shared_ptr 或另一个 weak_ptr 对象构造, 它的构造和析构不会引起引用记数的增加或减少。

    weak_ptr是用来解决shared_ptr相互引用时的死锁问题:如果两个shared_ptr相互引用,那么这两个指针的引用计数永远不可能下降为0,资源永远不会释放。

    weak_ptr是对对象的一种弱引用,不会增加或减少对象的引用计数,与shared_ptr之间可以相互转化,shared_ptr可以直接赋值给它,它可以通过调用lock函数来获得shared_ptr。

    good good study, day day up.

    顺序 选择 循环 总结

  • 相关阅读:
    如何将应用安装到/system/app下
    WPF Perf: RenderCapability.Tier & DesiredFrameRate
    DataGridComboBoxColumn为什么就不能在Binding的时候引用其他Named Element了呢?
    A366T使用技巧
    在XAML里面引用枚举值的注意点
    高斯消元bzoj1013球形空间产生器
    欧拉函数bzoj2818简单推导
    链剖进阶ing填坑NOIP2013货车运输
    .net 2.0 BackgroundWorker 文章三篇
    19号晚21号上午
  • 原文地址:https://www.cnblogs.com/Braveliu/p/12247042.html
Copyright © 2011-2022 走看看