[boost::enable_shared_from_this] in inheritance tree throws bad_weak_ptr exception
I need to create a class hierarchy where every level inherits from
boost::enable_shared_from_this. I am having problems caused, I believe, by
having more than one copy of enable_shared_from_this.
I am using boost 1.34.1 with VS2003 and VS2005, with and without ICL9.1,
always getting the same result: a boost::bad_weak_ptr exception.
#include
I need to create a class hierarchy where every level inherits from boost::enable_shared_from_this. I am having problems caused, I believe, by having more than one copy of enable_shared_from_this.
I am using boost 1.34.1 with VS2003 and VS2005, with and without ICL9.1, always getting the same result: a boost::bad_weak_ptr exception.
You're right, multiple copies of enable_shared_from_this are unfortunately not supported.
#include
#include using namespace boost;
struct A : enable_shared_from_this<A> { shared_ptr<A> getA() {return enable_shared_from_this<A>::shared_from_this();} }; struct B : A, enable_shared_from_this<B> { shared_ptr<B> getB() {return enable_shared_from_this<B>::shared_from_this();} };
In this case, you can use struct B : A { shared_ptr<B> getB() { return dynamic_pointer_cast<B>( shared_from_this() ); } }; after making A polymorphic.
Peter Dimov wrote:
In this case, you can use
struct B : A { shared_ptr<B> getB() { return dynamic_pointer_cast<B>( shared_from_this() ); } };
after making A polymorphic.
Is there any reason you couldn't use static_pointer_cast? You know for a fact that the cast to B will work, as getB is a member function of B. I think this will also remove the need to make A polymorphic.
Andrew Holden:
Peter Dimov wrote:
In this case, you can use
struct B : A { shared_ptr<B> getB() { return dynamic_pointer_cast<B>( shared_from_this() ); } };
after making A polymorphic.
Is there any reason you couldn't use static_pointer_cast? You know for a fact that the cast to B will work, as getB is a member function of B. I think this will also remove the need to make A polymorphic.
static_pointer_cast will work too, as long as no virtual inheritance is involved (D: C, D: B, B: A, C: A).
participants (3)
-
Andrew Holden
-
dariomt@gmail.com
-
Peter Dimov