how boost::~shared_ptr works?

前端 未结 2 561
闹比i
闹比i 2021-02-13 12:53

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

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


        
2条回答
  •  你的背包
    2021-02-13 13:58

    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.

提交回复
热议问题