how boost::~shared_ptr works?

笑着哭i 提交于 2019-12-03 10:56:22

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).

The shared_ptr class template has a member of class type shared_count, which in turn has a member of type pointer to class sp_counted_base. The constructor template for class shared_count assigns a pointer to an instance of the class template sp_counted_impl_p to this member which is templated by the type of the constructor argument, not by the shared_ptr::value_type. sp_counted_base has a pure virtual member function dispose which is overwritten by sp_counted_impl_p. Because sp_counted_impl_p knows the type B in your example, it can delete it without access to the base class destructor, and because it uses virtual dispatch, the type is determined at runtime. This method requires a combination of parametric and subtype polymorphism.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!