how boost::~shared_ptr works?

前端 未结 2 532
闹比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:36

    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 does not 'simply' have a constructor expecting a T * but :

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

提交回复
热议问题