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"<mccorywork@gmail.com>; Date: Sat, Mar 14, 2009 04:29 AM To: "Boost-users"<boost-users@lists.boost.org>; Subject: [Boost-users] shared_ptr,auto_ptr and enable_shared_from_this confusion I'm having some trouble with using shared_ptr, auto_ptr and enable_shared_from_this with my inheritance layout. The following is how my inheritance is laid out. // Inheritance Layout // // boost::enable_shared_from_this<T> // ^ // | // Base --------> IBase // ^ // | // Derived -----> IDerived In my test code (that I've included with this email) the following two ways to instantiate a shared pointer is equivalent. boost::shared_ptr<IDerived> r (new Derived()); std::auto_ptr<IDerived> k (new Derived); boost::shared_ptr<IDerived> l (k.release()); When creating the shared_ptr, both declaration take me to line 185 of shared_ptr.hpp. Instantiating the shared_ptr directly then takes me to line 105 of shared_ptr.hpp. Though when instantiating by releasing the auto_ptr into the shared_ptr I'm taken to line 129. It seems to me that I should be going to the same line of code. This wouldn't be such a problem except when trying to call shared_from_this() an exception is thrown if my shared_ptr was created from an auto_ptr. Is this behavior correct? Ryan
participants (1)
-
张亚霏