Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2007-10-02 12:19:07


>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 <boost/shared_ptr.hpp>
> #include <boost/enable_shared_from_this.hpp>
>
> 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.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net