C++ unique_ptr怎么用_C++独占智能指针所有权转移教程

admin 百科 12
std::unique_ptr是C++中独占式智能指针,通过移动语义实现所有权转移,使用make_unique创建并自动管理内存,防止泄漏。1. 推荐用make_unique初始化;2. 禁止拷贝但可std::move转移所有权;3. 函数传参时值传递转移、引用传递借用;4. 可安全返回,编译器优化移动或RVO。

C++ unique_ptr怎么用_C++独占智能指针所有权转移教程-第1张图片-佛山资讯网

C++ 中的 std::unique_ptr 是一种独占式智能指针,用于确保同一时间只有一个指针拥有对动态分配对象的所有权。它能自动管理内存,在指针销毁时自动释放所指向的对象,防止内存泄漏。由于其“独占性”,不能通过普通赋值或拷贝构造函数来共享所有权,但可以通过所有权转移(移动语义)将控制权交给另一个 unique_ptr。

1. 基本用法:创建和使用 unique_ptr

使用 std::make_unique 是创建 unique_ptr 的推荐方式(C++14 起支持):

#include <memory>
#include <iostream>

int main() {
    // 创建一个指向 int 的 unique_ptr
    auto ptr = std::make_unique<int>(42);
    std::cout << *ptr << std::endl;  // 输出: 42

    // 创建一个指向自定义类对象的 unique_ptr
    struct MyClass {
        void say_hello() { std::cout << "Hello!" << std::endl; }
    };
    auto obj = std::make_unique<MyClass>();
    obj->say_hello();  // 调用成员函数
}

登录后复制

也可以使用构造函数直接初始化,但不推荐手动 new:

std::unique_ptr<int> ptr(new int(10));  // 不推荐,除非必要

登录后复制

2. 所有权转移:移动而非复制

unique_ptr 禁止拷贝,但允许通过 std::move 进行移动,实现所有权转移:

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

auto ptr1 = std::make_unique<int>(100);
// auto ptr2 = ptr1;        // 错误!禁止拷贝
auto ptr2 = std::move(ptr1); // 正确!所有权从 ptr1 转移到 ptr2

if (ptr1 == nullptr) {
    std::cout << "ptr1 is now null" << std::endl;  // 会输出这句
}
if (ptr2 != nullptr) {
    std::cout << "ptr2 owns the value: " << *ptr2 << std::endl;
}

登录后复制

移动后,原指针变为 nullptr,不再拥有对象,资源由新指针接管。

标签: ai c++ ios stream

发布评论 0条评论)

还木有评论哦,快来抢沙发吧~