提交 2337187e 编写于 作者: 邹晓航

bug fix

上级 525308f2
...@@ -107,20 +107,22 @@ namespace TinySTL{ ...@@ -107,20 +107,22 @@ namespace TinySTL{
class shared_ptr{ class shared_ptr{
public: public:
typedef T element_type; typedef T element_type;
private:
template<class Type>
using ref_t = Detail::ref_t < Type >;
public: public:
explicit shared_ptr(T *p = nullptr) :ref_(new ref_t<T>(p)){} explicit shared_ptr(T *p = nullptr) :ref_(new ref_t<T>(p)){}
template<class D> template<class D>
shared_ptr(T *p, D del) : ref_(new ref_t<T>(p, del)){} shared_ptr(T *p, D del) : ref_(new ref_t<T>(p, del)){}
shared_ptr(const shared_ptr& sp){ shared_ptr(const shared_ptr& sp){
ref_ = sp.ref_; copy_ref(sp.ref_);
++(*ref_);
} }
shared_ptr& operator = (const shared_ptr& sp){ shared_ptr& operator = (const shared_ptr& sp){
if (this != &sp){ if (this != &sp){
decrease_ref(); decrease_ref();
ref_ = sp.ref_; copy_ref(sp.ref_);
} }
return *this; return *this;
} }
...@@ -136,16 +138,17 @@ namespace TinySTL{ ...@@ -136,16 +138,17 @@ namespace TinySTL{
operator bool() const{ return get() != nullptr; } operator bool() const{ return get() != nullptr; }
private: private:
void decrease_ref(){ void decrease_ref(){
if (ref_){ if (ref_->get_data()){
--(*ref_); --(*ref_);
if (use_count() == 0) if (use_count() == 0)
delete ref_; delete ref_;
} }
} }
void copy_ref(ref_t<T> *r){//ȷĿref_t
ref_ = r;
++(*ref_);
}
private: private:
template<class Type>
using ref_t = Detail::ref_t < Type > ;
ref_t<T> *ref_; ref_t<T> *ref_;
}; };
template<class T1, class T2> template<class T1, class T2>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册