C++智能指针

在C++中,智能指针(Smart Pointer)是一种用于自动管理动态内存的对象。智能指针通过自动管理内存的分配和释放,帮助程序员避免内存泄漏和其他与内存管理相关的问题。C++11标准引入了智能指针库,提供了几种常用的智能指针类型,包括 std::unique_ptrstd::shared_ptrstd::weak_ptr

1. std::unique_ptr

std::unique_ptr是一种独占所有权的智能指针,即一个 std::unique_ptr对象独占其所指向的资源。不能复制 std::unique_ptr,但可以移动它。

示例代码

#include <iostream>
#include <memory> // 包含智能指针库

class MyClass {
public:
    MyClass() {
        std::cout << "MyClass constructor" << std::endl;
    }
    ~MyClass() {
        std::cout << "MyClass destructor" << std::endl;
    }
    void display() {
        std::cout << "Hello from MyClass" << std::endl;
    }
};

int main() {
    std::unique_ptr<MyClass> ptr1 = std::make_unique<MyClass>(); // 创建 unique_ptr
    ptr1->display();

    // std::unique_ptr<MyClass> ptr2 = ptr1; // 错误:不能复制 unique_ptr
    std::unique_ptr<MyClass> ptr2 = std::move(ptr1); // 移动 unique_ptr
    ptr2->display();

    // ptr1 现在为空,不能再使用
    if (!ptr1) {
        std::cout << "ptr1 is null" << std::endl;
    }

    return 0;
}

2. std::shared_ptr

std::shared_ptr 是一种共享所有权的智能指针,即多个 std::shared_ptr 对象可以共享同一个资源。当最后一个 std::shared_ptr 被销毁时,资源才会被释放。

示例代码

#include <iostream>
#include <memory> // 包含智能指针库

class MyClass {
public:
    MyClass() {
        std::cout << "MyClass constructor" << std::endl;
    }
    ~MyClass() {
        std::cout << "MyClass destructor" << std::endl;
    }
    void display() {
        std::cout << "Hello from MyClass" << std::endl;
    }
};

int main() {
    std::shared_ptr<MyClass> ptr1 = std::make_shared<MyClass>(); // 创建 shared_ptr
    {
        std::shared_ptr<MyClass> ptr2 = ptr1; // 共享所有权
        ptr2->display();
        std::cout << "ptr2 use count: " << ptr2.use_count() << std::endl; // 显示引用计数
    } // ptr2 离开作用域,引用计数减1

    std::cout << "ptr1 use count: " << ptr1.use_count() << std::endl; // 显示引用计数
    ptr1->display();

    return 0;
}

3. std::weak_ptr

std::weak_ptr 是一种不拥有资源的弱引用智能指针。它与 std::shared_ptr 配合使用,用于打破循环引用。std::weak_ptr 不影响资源的引用计数。

示例代码

#include <iostream>
#include <memory> // 包含智能指针库

class MyClass {
public:
    MyClass() {
        std::cout << "MyClass constructor" << std::endl;
    }
    ~MyClass() {
        std::cout << "MyClass destructor" << std::endl;
    }
    void display() {
        std::cout << "Hello from MyClass" << std::endl;
    }
};

int main() {
    std::shared_ptr<MyClass> sharedPtr = std::make_shared<MyClass>(); // 创建 shared_ptr
    std::weak_ptr<MyClass> weakPtr = sharedPtr; // 创建 weak_ptr

    if (auto tempPtr = weakPtr.lock()) { // 尝试锁定 weak_ptr
        tempPtr->display();
        std::cout << "tempPtr use count: " << tempPtr.use_count() << std::endl; // 显示引用计数
    } else {
        std::cout << "Resource no longer exists" << std::endl;
    }

    sharedPtr.reset(); // 释放 shared_ptr 所有权

    if (auto tempPtr = weakPtr.lock()) { // 再次尝试锁定 weak_ptr
        tempPtr->display();
    } else {
        std::cout << "Resource no longer exists" << std::endl;
    }

    return 0;
}

总结

  • std::unique_ptr:独占所有权,不能复制,但可以移动。
  • std::shared_ptr:共享所有权,多个 shared_ptr 可以共享同一个资源,使用引用计数管理资源。
  • std::weak_ptr:弱引用,不拥有资源,与 shared_ptr 配合使用,主要用于打破循环引用。

智能指针通过自动管理内存的分配和释放,极大地减少了手动管理内存的复杂性和错误风险。使用智能指针是现代C++编程的一种最佳实践。

  • 10
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值