Re: [Boost-users] shared_ptr, auto_ptr and enable_shared_from_this confusion
shared_ptr's constructor in line 168 calls 'boost::detail::sp_enable_shared_from_this( pn, p, p );'.
'boost::shared_ptr<IDerived> c(new Derived());' is dispatched to 'sp_enable_shared_from_this' in line 86. pay attension to the second parameter, it can accept all types that inherit 'enable_shared_from_this<T>', so does 'Derived*'.
While, 'boost::shared_ptr<IDerived> c((IDerived*)new Derived());' is dispatched to 'sp_enable_shared_from_this' in line 111, bacause the static type IDerived* has nothing to do with 'enable_shared_from_this<T>' and can not be implicitly converted to 'enable_shared_from_this<T>'.
Then check out the difference between the two 'sp_enable_shared_from_this'. It is obvious that the second usage shall cause a BOOST_ASSERT failed in 'enable_shared_from_this::shared_from_this', so an exception is raised.
However, I think the root reason is not the language nor boost, but the design of class IDerived. Its root and root1 seem a little bit irrational because there is not direct relationship between IDerived and Base(IBase).
------------------ Original ------------------
From: "Ryan McConnehey"
participants (1)
-
张亚霏