如何提高:: shared_ptr的〜作品? [英] how boost::~shared_ptr works?

查看:105
本文介绍了如何提高:: shared_ptr的〜作品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读时的C ++标准库超越:介绍升压,我得到了一个很有趣的例子:

  A级
{
上市:
    虚拟无效唱()= 0;
保护:
    虚拟〜A(){};
};B类:公开发行A
{
上市:
    虚拟无效唱()
    {
       性病::法院LT&;< 不要再ㄙ所以拉<<的std :: ENDL ;;
    }
};

和我做一些测试:

  INT的main()
{// 1
性病:: auto_ptr的< A> (新B); //将无法编译,错误:'虚拟A ::〜A()'被保护// 2
A * PA =新型B;
每年删除; //将无法编译,错误:'虚拟A ::〜A()'被保护
删除(dynamic_cast的< B *>(PA)); //好// 3
提高:: shared_ptr的< A> (新B); // OK}

我很好奇,这里是什么shared_ptr的〜怎么工作的?
它是如何演绎派生B类?

感谢您的帮助!

感谢所有,
我写一个简单的示例如何〜shared_ptr的作品

 类sp_counted_base
{
上市:
    虚拟〜sp_counted_base(){}
};模板< typename的T>
类sp_counted_base_impl:公共sp_counted_base
{
上市:
    sp_counted_base_impl(T * T):T_(T){}
    〜sp_counted_base_impl(){删除T_;}
私人的:
    T * T_;
};
类SHARED_COUNT
{
上市:
    静态INT count_;
    模板< typename的T>
    SHARED_COUNT(T * T):
        T_(新sp_counted_base_impl< T>(T))
    {
        count_ ++;
    }
    无效发行()
    {
         - 计数_;
        如果(0 == count_)删除T_;
    }
    〜SHARED_COUNT()
    {
        发布();
    }
私人的:
    sp_counted_base * T_;
};
INT SHARED_COUNT :: count_(0);模板< typename的T>
类myautoptr
{
上市:
    模板< typename的Y'GT;
    myautoptr(Y * Y):SC_(Y),T_(Y){}
    〜myautoptr(){sc_.release();}
私人的:
    SHARED_COUNT SC_;
    T * T_;
};诠释的main()
{
    myautoptr< A> (新B);
}

关键是:


  1. 模板构造函数

  2. 在〜shared_ptr的不删除该资源,它是由SHARED_COUNT删除


解决方案

奇怪的是,这里的关键是不要的boost :: shared_ptr的析构函数,但它的构造(S)。

如果你看看升压/ shared_ptr.hpp ,您将看到的shared_ptr< T> 不'简单的有一个构造函数期待 T * ,但

 模板< Y类>
明确的shared_ptr(Y * P);

// 3 当你构建一个的boost :: shared_ptr的 B * ,无中转 A * 发生,而的shared_ptr 内部与建实际 B 键入。一旦对象的破坏,删除发生在 B 指针(而不是通过一个基类指针)。

when reading "Beyond the C++ Standard Library: An Introduction to Boost " ,I got a very interesting example:

class A  
{  
public:  
    virtual void sing()=0;  
protected:  
    virtual ~A() {};  
};

class B : public A
{  
public:  
    virtual void sing(  )  
    {  
       std::cout << "Do re mi fa so la"<<std::endl;;  
    }  
};  

and I do some testing:

int main()
{  

//1  
std::auto_ptr<A> a(new B); //will not compile ,error: ‘virtual A::~A()’ is protected

//2
A *pa = new B;
delete pa;  //will not compile ,error: ‘virtual A::~A()’ is protected
delete (dynamic_cast<B*>(pa)); //ok

//3 
boost::shared_ptr<A> a(new B);//ok

}

what I am very curious here is how ~shared_ptr works? how it deduce the derived class B ?

Thanks advance for your help!

thanks all, I write a simple sample about how ~shared_ptr works

class sp_counted_base
{
public:
    virtual ~sp_counted_base(){}
};

template<typename T>
class sp_counted_base_impl : public sp_counted_base
{
public:
    sp_counted_base_impl(T *t):t_(t){}
    ~sp_counted_base_impl(){delete t_;}
private:
    T *t_;
};


class shared_count
{
public:
    static int count_;
    template<typename T>
    shared_count(T *t):
        t_(new sp_counted_base_impl<T>(t))
    {
        count_ ++;
    }
    void release()
    {
        --count_;
        if(0 == count_) delete t_;
    }
    ~shared_count()
    {
        release();
    }
private:
    sp_counted_base *t_;
};
int shared_count::count_(0);

template<typename T>
class myautoptr
{
public:
    template<typename Y>
    myautoptr(Y* y):sc_(y),t_(y){}
    ~myautoptr(){ sc_.release();}
private:
    shared_count sc_;
    T *t_;
};

int main()
{
    myautoptr<A> a(new B);
}

the key is:

  1. template construct function
  2. the resource not deleted in ~shared_ptr ,it is deleted by shared_count

解决方案

Surprisingly, the key here is not boost::shared_ptr destructor but its constructor(s).

If you look into boost/shared_ptr.hpp, you will see that shared_ptr<T> does not 'simply' have a constructor expecting a T * but :

template<class Y>
explicit shared_ptr( Y * p );

In //3 when you construct a boost::shared_ptr from a B *, no conversion to A * takes place, and the shared_ptr internals are built with the actual B type. Upon destruction of the object, deletion occurs on a B pointer (not through a base class pointer).

这篇关于如何提高:: shared_ptr的〜作品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆