c++中unique_ptr的使用方法_c++ unique_ptr智能指针用法详解

unique_ptr是c++11引入的独占式智能指针,通过move语义转移所有权,配合make_unique可安全高效管理动态对象,防止内存泄漏。

c++中unique_ptr的使用方法_c++ unique_ptr智能指针用法详解

在C++中,unique_ptr 是一种智能指针,用于管理动态分配的对象,确保同一时间只有一个指针拥有该对象的所有权。当 unique_ptr 被销毁时,它所指向的对象也会自动被释放,从而有效防止内存泄漏。它是 C++11 引入的资源管理工具之一,适用于需要独占所有权的场景。

基本定义与初始化

要使用 unique_ptr,需要包含头文件 <memory>。它的基本语法如下:

 std::unique_ptr<T> ptr; std::unique_ptr<T> ptr(new T); auto ptr = std::make_unique<T>(...); // 推荐方式(C++14起) 

建议优先使用 std::make_unique,因为它更安全、更高效,并能避免重复调用 new。

示例:

立即学习C++免费学习笔记(深入)”;

 #include <memory> #include <iostream> <p>struct MyClass { int value; MyClass(int v) : value(v) { std::cout << "构造n"; } ~MyClass() { std::cout << "析构n"; } };</p><p>auto ptr = std::make_unique<MyClass>(42); // 自动管理生命周期</p>

所有权唯一,不可复制

unique_ptr 的核心特性是独占所有权,因此不支持拷贝构造和拷贝赋值。

 auto ptr1 = std::make_unique<int>(10); // auto ptr2 = ptr1;        // 错误:不能复制 auto ptr2 = std::move(ptr1); // 正确:通过 move 转移所有权 

执行 move 操作后,ptr1 变为 nullptr,ptr2 拥有对象。这是实现资源安全转移的关键机制。

作为函数参数和返回值

在函数间传递 unique_ptr 时,通常使用移动语义。

作为返回值:

c++中unique_ptr的使用方法_c++ unique_ptr智能指针用法详解

法语写作助手

法语助手旗下的AI智能写作平台,支持语法、拼写自动纠错,一键改写、润色你的法语作文。

c++中unique_ptr的使用方法_c++ unique_ptr智能指针用法详解31

查看详情 c++中unique_ptr的使用方法_c++ unique_ptr智能指针用法详解

 std::unique_ptr<MyClass> createObject() {     return std::make_unique<MyClass>(100); } <p>auto obj = createObject(); // 接收所有权</p>

作为参数(若需转移所有权):

 void takeOwnership(std::unique_ptr<MyClass> ptr) {     std::cout << ptr->value << "n"; } // 函数结束时自动析构 <p>takeOwnership(std::move(obj)); // obj 失去所有权</p>

如果只是临时访问对象,应使用指针或引用:

 void useobject(const MyClass* ptr) {     if (ptr) std::cout << ptr->value << "n"; } // 或 void useObject(const MyClass& ref) {     std::cout << ref.value << "n"; } 

自定义删除器

默认情况下,unique_ptr 使用 delete 释放资源。对于特殊资源(如 FILE*、C API 对象),可指定自定义删除器。

 // 管理 C 风格文件 auto file_deleter = [](FILE* f) {     if (f) fclose(f); }; std::unique_ptr<FILE, decltype(file_deleter)> file(fopen("test.txt", "w"), file_deleter); <p>if (file) { fprintf(file.get(), "Hellon"); } // 离开作用域时自动关闭文件</p>

自定义删除器可以是函数指针、Lambda 或仿函数,使 unique_ptr 更具扩展性。

基本上就这些。unique_ptr 设计简洁,强调“单一所有权”,配合 move 语义和 make_unique,能写出既安全又高效的代码。只要记住它不能复制,需要用 move 来转移,日常使用就不会出错。

上一篇
下一篇
text=ZqhQzanResources