Boost logo

Boost Users :

From: sashan (sashang_at_[hidden])
Date: 2003-10-21 19:39:25

I'm trying to use enable_shared_from_this with my classes that have an
inheritance structure and a virtual function.
I'd like to get a shared pointer to the descendant class B. I get a
compile error when I try to dynamic cast the shared ptr of type A to a
shared_ptr of type B. See the attached sample.


#include <iostream>
#include <boost/enable_shared_from_this.hpp>

using namespace std;
using namespace boost;

class A :
        public enable_shared_from_this<A>
        A(){ cout << "A" << endl; }
        virtual void f() =0;

class B :
        public A

        B() { cout << "B" << endl;}
        virtual void f()
                shared_ptr<A> pA(enable_shared_from_this<A>::shared_from_this());
                shared_ptr<B> pB1(dynamic_pointer_cast < shared_ptr<B> >(pA));
void main()
        B b;

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at