Weak_ptr实现

前言:

循环引用实例:

/*
    @author:wcx
    @task:shared_ptr循环引用实例
*/
#include<bits/stdc++.h>
using namespace std;
struct node{
    shared_ptr<node> t;
};
int main(){
    shared_ptr<node> a(new node);
    shared_ptr<node> b(new node);
    a->t=b;
    b->t=a;
}

当a删除的时候,因为自身引用计数为2(b指向了自己),所有减为1不会释放内部成员,也就是指向的b引用计数不会变化,释放b的时候也一样,所以到最后两者都不会释放,计数为1。

解决方法:

用weak_ptr解决,它是辅助shared_ptr的一个结构,不增加引用计数,准确意义来说它不是个指针,它不能随意指向。

/*
    @author:wcx
    @task:shared_ptr循环引用解决
*/
#include<bits/stdc++.h>
using namespace std;
struct node{
    weak_ptr<node> t;
};
int main(){
    shared_ptr<node> a(new node);
    shared_ptr<node> b(new node);
    a->t=b;
    b->t=a;
}
Weak_ptr实现:
/*
    @author:wcx
    @task:weak_ptr实现
*/
#include<bits/stdc++.h>
using namespace std;
namespace P{
    template <typename T>
    class Weak_ptr{
        //不占用引用计数
    public:
        Weak_ptr(const shared_ptr<T> da):s(da.ptr){}
        ~Weak_ptr(){}
    protected:
        T *s;
    };
}
int main(){
    using namespace P;
    Shared_ptr<int> a(new int);
    Weak_ptr<int> t(a);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
weak_ptr是C++11引入的一种智能指针,它可以用来解决shared_ptr的循环引用问题。weak_ptr指向一个shared_ptr管理的对象,但它不会增加该对象的引用计数,也不控制该对象的生命周期。如果shared_ptr被销毁了,那么weak_ptr就会变成空指针。 下面是一个简单的weak_ptr实现: ```c++ template<typename T> class shared_ptr; template<typename T> class weak_ptr { public: weak_ptr() : m_ptr(nullptr), m_refCount(nullptr) {} weak_ptr(const shared_ptr<T>& sp) : m_ptr(sp.m_ptr), m_refCount(sp.m_refCount) { if (m_refCount) { m_refCount->weakCount++; } } weak_ptr(const weak_ptr<T>& wp) : m_ptr(wp.m_ptr), m_refCount(wp.m_refCount) { if (m_refCount) { m_refCount->weakCount++; } } ~weak_ptr() { reset(); } weak_ptr<T>& operator=(const shared_ptr<T>& sp) { reset(); m_ptr = sp.m_ptr; m_refCount = sp.m_refCount; if (m_refCount) { m_refCount->weakCount++; } return *this; } weak_ptr<T>& operator=(const weak_ptr<T>& wp) { reset(); m_ptr = wp.m_ptr; m_refCount = wp.m_refCount; if (m_refCount) { m_refCount->weakCount++; } return *this; } void reset() { if (m_refCount) { m_refCount->weakCount--; if (m_refCount->weakCount == 0 && m_refCount->refCount == 0) { delete m_refCount; delete m_ptr; } } m_ptr = nullptr; m_refCount = nullptr; } shared_ptr<T> lock() const { if (expired()) { return shared_ptr<T>(); } else { return shared_ptr<T>(*this); } } bool expired() const { return use_count() == 0; } long use_count() const { if (m_refCount) { return m_refCount->refCount; } else { return 0; } } private: T* m_ptr; shared_ref_count* m_refCount; }; ``` 在实现中,我们需要维护一个shared_ref_count类,用来计数一个对象的引用次数和weak_ptr的数量。 ```c++ class shared_ref_count { public: shared_ref_count() : refCount(1), weakCount(0) {} long refCount; long weakCount; }; ``` 在weak_ptr的构造函数中,我们需要将weakCount增加。在析构函数中,我们需要调用reset()方法,将weakCount减少,并在refCount和weakCount都为0时释放资源。 在reset()方法中,我们首先将weakCount减少,然后判断refCount和weakCount是否都为0,如果是,则释放资源。 在lock()方法中,我们首先判断是否过期(即use_count()是否为0),如果是,则返回一个空的shared_ptr,否则返回一个新的shared_ptr。 在expired()方法中,我们判断use_count()是否为0。 在use_count()方法中,我们返回refCount的值。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值