C++学习笔记(30):智能指针(unique_ptr)

核心本质

  • 独占所有权: 同一时间,只有一个unique_ptr 能管理同一个对象,禁止拷贝,只能移动
  • 自动释放: 离开作用域时自动调用delete / delete\[\],永不内存泄漏(正常流程下)
  • 零额外开销: 和原始指针性能几乎一致,没有引用计数等额外成本
    注:要包含 #include

unique_ptr的使用

我们可以使用unique_ptr 类的构造函数创建智能指针对象,来管理由 new,new\[\],创建出来的动态对象

unique_ptr 特性

unique_ptr特点:

  • 同时只能有一个unique_ptr 对象来持有动态对象
  • unique_ptr 对象不支持默认拷贝,默认赋值语义
  • unique_ptr 对象支持移动拷贝,移动赋值语义

因此,我们知道,unique_ptr 对象不能以值的方式作为函数参数,也不能存储到STL的容器中(容器要求元素必须能够拷贝)

arduino 复制代码
#if 0
#include <iostream>
#include <memory>
#include <vector>
using namespace std;

class Person
{
public:
    Person()
    {
        cout << "构造函数" << endl;
    }

    ~Person()
    {
        cout << "析构函数" << endl;
    }
};

void test()
{
    unique_ptr<Person> up1(new Person);
    unique_ptr<Person> up2(new Person);

    // 1. 禁止拷贝、赋值
    // unique_ptr(const unique_ptr&)            = delete;
    // unique_ptr& operator=(const unique_ptr&) = delete;

    // 2. 允许移动拷贝、赋值
    unique_ptr<Person> up3(move(up1));  // 移动拷贝
    up2 = move(up3);  // 移动赋值

    // 由此, unique_ptr 不允许作为容器元素
    // vector<unique_ptr<Person>> vec;
    // vec.push_back(up1);
}

int main()
{
    test();
    return 0;
}

#endif

unique_ptr 自定义删除器

unique_ptr 可用于管理 new 出来的动态对象,也可以管理其他需要手动关闭的资源。例如:文件对象。

由于 unique_ptr 默认使用 delete、delete\[\] 来释放被管理的资源。所以,当管理的对象不能通过 delete、delete\[\] 来释放时,就需要自定义删除器。

c 复制代码
    #if 1

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <functional>
using namespace std;

class Person
{
    public:
        Person()
    {
        cout << "构造函数" << endl;
    }

        ~Person()
    {
    cout << "析构函数" << endl;
    }
    };

struct Deleter
{
    void operator()(FILE* fp)
{
    cout << "文件被自动关闭" << endl;

    if (fp != nullptr)
    {
        fclose(fp);
        fp = nullptr;
        }
    }
};

void my_deleter(FILE* fp)
{
    cout << "文件被自动关闭" << endl;

    if (fp != nullptr)
{
    fclose(fp);
    fp = nullptr;
    }
}


void test()
{
// 1. 函数对象作为删除器
// unique_ptr<FILE, Deleter> up(fopen("./demo.txt", "w"), Deleter());
// unique_ptr<FILE, function<void(FILE*)>> up(fopen("./demo.txt", "w"),Deleter());

// 2. 普通函数作为删除器
// unique_ptr<FILE, decltype(&my_deleter)> up(fopen("./demo.txt", "w"), my_deleter);
// unique_ptr<FILE, void(*)(FILE *)> up(fopen("./demo.txt", "w"), my_deleter);
// unique_ptr<FILE, function<void(FILE*)>> up(fopen("./demo.txt", "w"), my_deleter);

// 3. 匿名函数作为删除器
unique_ptr<FILE, function<void(FILE *)>> up(fopen("./demo.txt", "w"), [](FILE *fp) {
        cout << "文件被自动关闭" << endl;

        if (fp != nullptr)
        {
            fclose(fp);
            fp = nullptr;
        }
    });


if (!up)
{
    cout << "文件打开失败" << endl;
    return;
}
fputs("hello world\n", up.get());
}

int main()
{
    test();
    return 0;
}

    #endif
相关推荐
tdtsmt33 分钟前
穿戴硬件量产工艺实录:天地通电子智能手表主板 SMT 贴片案
c++
m0_734571764 小时前
深入理解C++ 析构函数<一>基本概念
开发语言·c++
t-think5 小时前
C++ string 类(上)—— string类初识与常用接口详解
开发语言·c++
cpp_learners5 小时前
C++ 实现责任链模式(Chain of Responsibility):从一堆 if-else 到可插拔的处理管道
开发语言·c++·责任链模式
郝学胜_神的一滴5 小时前
C++11 工程级应用 12:编译期类型魔法,干掉重复与臃肿的代码
c++·visual studio
David猪大卫7 小时前
【C++修炼】哈希表的实现
数据结构·c++·笔记·学习·算法·哈希算法·散列表
广州山泉婚姻7 小时前
列表初始化:C++11全新初始化体系
c++·人工智能
莫生灬灬7 小时前
灵码 LingBuilder:用中文写代码,确定性生成真实 C++ 工程(开源)
c++·开源·mfc
浅念-7 小时前
C++ Protobuf 入门实战:基于通讯录项目吃透proto3语法与序列化
linux·服务器·网络·c++·protobuf·proto·proto3
汉克老师9 小时前
CSP-J 初赛(以满分为目标):第三十九课《数学与逻辑②——阶乘、排列数与组合数:公式到底从哪里来?》
c++·csp-j·小学生·学c++编程