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
#include <iostream>
#include <memory>
#include
AMDG Ryan McConnehey wrote:
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?
The difference is that when you use direct construction, the pointer that you pass to the constructor is of type Derived*, but when you use auto_ptr, the constructor argument is of type IDerived. For enable_shared_from_this to work, the static type of the pointer passed to the shared_ptr constructor must inherit from enable_shared_from_this. In Christ, Steven Watanabe
participants (2)
-
Ryan McConnehey
-
Steven Watanabe