智能指针之shared_ptr和unique_ptr实现

#include <iostream>
#include <string>
using namespace std;
 
class Delete
{
public:
    template <typename T>
    void operator()(T *p) const
    {
        delete p;
    }
};
template <typename T, typename D = Delete>
class unique_ptr
{
public:
    explicit unique_ptr(T *pp = nullptr, const D &dd = D())
        : un_ptr(pp), del(dd)
    {
    }
    ~unique_ptr()
    {
        del(un_ptr);
    }
    /* 不支持拷贝与赋值   */
    unique_ptr(const unique_ptr &) = delete;
    unique_ptr &operator=(const unique_ptr &) = delete;
 
    /*可以拷贝或者赋值一个将要被销毁的 unique_ptr(右值引用)*/
    unique_ptr(unique_ptr &&right_value) : un_ptr(right_value.un_ptr), del(std::move(right_value.del))
    {
        right_value.un_ptr = nullptr;
    }
    unique_ptr &operator=(unique_ptr &&right_value) noexcept
    {
        if (this != &right_value)
        {
            std::cout << "operator && right_value " << std::endl;
            del(*this);
            un_ptr = right_value.un_ptr;
            del = std::move(right_value.del);
            right_value.un_ptr = nullptr;
        }
        return *this;
    }
    //u.release()   u 放弃对指针的控制权,返回指针,并将 u 置为空
    T *release()
    {
        T *tmp = un_ptr;
        un_ptr = nullptr;
        return tmp;
    }
    /*
    u.reset()   释放u指向的对象
    u.reset(q)  如果提供了内置指针q,就令u指向这个对象
    u.reset(nullptr) 将 u 置为空
    */
    void reset() { del(un_ptr); }
    void reset(T *q)
    {
        if (un_ptr)
        {
            del(un_ptr);
            un_ptr = q;
        }
        else
            un_ptr = nullptr;
    }
    void swap(unique_ptr &other) noexcept
    {
        using std::swap;
        swap(un_ptr, other.un_ptr);
        swap(del, other.del);
    }
    T *get() { return un_ptr; }
    D &get_deleter() { return del; }
    T &operator*() { return *un_ptr; }
    T *operator->() { return un_ptr; }
 
private:
    T *un_ptr = nullptr;
    D del;
};
 
struct Foo
{
    Foo() { std::cout << "Foo()\n"; }
    ~Foo() { std::cout << "~Foo()\n"; }
    Foo(const Foo &) { std::cout << "Foo copy ctor\n"; }
    Foo(Foo &&) { std::cout << "Foo move ctor\n"; }
};
struct Fooo
{
    Fooo(int n = 0) noexcept : bar(n) { std::cout << "Fooo: constructor, bar = " << bar << '\n'; }
    ~Fooo() { std::cout << "Fooo: destructor, bar = " << bar << '\n'; }
    int GetBar() const noexcept { return bar; }
 
private:
    int bar;
};
struct D
{
    void bar() { std::cout << "Call deleter D::bar()...\n"; }
    void operator()(Foo *p) const
    {
        std::cout << "Call delete from function object...\n";
        delete p;
    }
};
 
int main()
{
    unique_ptr<string> p1(new string("my_unique_ptr"));
    cout << *p1 << endl;
    {
        std::cout << "======================\nunique_ptr constructor:\n";
        unique_ptr<Foo> up1;
        unique_ptr<Foo> up1b(nullptr);
        unique_ptr<Foo> up2(new Foo);
 
        Delete d;
        unique_ptr<Foo, Delete> up3(new Foo, d);
        unique_ptr<Foo, Delete &> up3b(new Foo, d);
        unique_ptr<Foo, Delete> up4(new Foo, Delete());
        unique_ptr<Foo> up5b(std::move(up2));
        unique_ptr<Foo, Delete> up6b(std::move(up3));
 
        unique_ptr<Foo> up7 = std::move(up5b);
        Foo *fp = up7.release();
        delete fp;
 
        up6b.reset(new Foo());
        up6b.reset(nullptr);
 
        unique_ptr<Fooo> up71(new Fooo(1));
        unique_ptr<Fooo> up72(new Fooo(2));
 
        up71.swap(up72);
 
        std::cout << "up71->val:" << up71->GetBar() << std::endl;
        std::cout << "up72->val:" << (up72.get())->GetBar() << std::endl;
 
        unique_ptr<Foo, D> up8(new Foo(), D());
        D &del = up8.get_deleter();
        del.bar();
    }
}
template<typename T>
class sharedptr {
public:
    //构造函数
    sharedptr(T *ptr):_ptr(ptr),_cnt(new int(1)) {
        cout << "构造" << endl;
    }
    //拷贝构造
    sharedptr(sharedptr<T> sp):_ptr(sp._ptr),_cnt(sp._cnt){
        cout << "拷贝构造" << endl;
        ++(*_cnt);
    }
    //赋值操作运算符
    sharedptr<T> operator=(sharedptr<T> sp) {
        cout << "赋值操作运算符" << endl;
        if (_ptr != sp._ptr) {
            if (--(*_cnt) == 0) {//计数器为0,则释放指针指向的对象
                delete _ptr;
                delete _cnt;
 
            }
            _ptr = sp._ptr;
            _cnt = sp._cnt;
            ++(*_cnt);
 
        }
        return *this;
    }
    T * operator->() {
        return _ptr;
    }
    T& operator*() {
        return *_ptr;
    }
    int getcnt() {
        return *_cnt;
    }
    ~sharedptr(){
        cout << "析构" << endl;
        if (--(*cnt) == 0) {
            delete _ptr;
            _ptr = nullptr;
            delete _cnt;
            _cnt = nullptr;
        }
    }
private:
    int *_cnt;
    T *_ptr;
};

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智能指针是C++中用于管理动态分配的内存的一种机制。它们可以自动地在不再需要时释放内存,从而避免内存泄漏和悬挂指针的问题。 shared_ptr是一种引用计数智能指针,它可以跟踪有多少个shared_ptr指向同一个对象,并在没有引用时自动释放内存。当创建shared_ptr时,它会增加引用计数,当销毁或重置shared_ptr时,它会减少引用计数。只有当引用计数为0时,才会真正释放内存。\[1\]shared_ptr可以通过构造函数接受一个指向动态分配对象的指针来创建,也可以使用std::make_shared函数来创建。\[2\] unique_ptr是一种独占智能指针,它拥有对动态分配对象的唯一所有权。当unique_ptr被销毁时,它会自动释放内存。unique_ptr不能被复制,但可以通过std::move函数进行转移所有权。\[3\]unique_ptr可以通过构造函数接受一个指向动态分配对象的指针来创建。 weak_ptr是一种弱引用智能指针,它指向由shared_ptr管理的对象,但不会增加引用计数。weak_ptr可以用于解决shared_ptr的循环引用问题,因为它不会导致对象无法释放。\[1\]weak_ptr可以通过shared_ptr的构造函数来创建。 auto_ptr是C++11之前的一种智能指针,它类似于unique_ptr,但有一些限制和问题。auto_ptr在复制时会转移所有权,这可能导致悬挂指针的问题。因此,auto_ptr已经被unique_ptr取代,不推荐使用。 总结来说,shared_ptr是引用计数智能指针unique_ptr是独占智能指针,weak_ptr是弱引用智能指针,而auto_ptr是已经过时的智能指针。它们各自有不同的用途和特点,可以根据具体的需求选择使用。 #### 引用[.reference_title] - *1* *2* *3* [C++11 解决内存泄露问题的智能指针shared_ptrunique_ptr、weak_ptr](https://blog.csdn.net/weixin_44120785/article/details/128714630)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值