Boost logo

Boost Users :

Subject: [Boost-users] How shared_from_this() work when it inherited from more than one time
From: R. P. Janaka (rpjanaka_at_[hidden])
Date: 2009-08-08 05:40:19


please can anyone explain the reason for the following behavior

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

using namespace std;

class A;
typedef boost::shared_ptr<A> A_sptr_t;

class B;
typedef boost::shared_ptr<B> B_sptr_t;

class A : public boost::enable_shared_from_this<A>
{
public:
A() {}
virtual ~A() {}
};

class B : public A, public boost::enable_shared_from_this<B>
{
public:
B() {}
virtual ~B() {}

void f(void)
{
cout << "this is working " <<
boost::enable_shared_from_this<B>::shared_from_this() << endl;
}
};

int main()
{
B_sptr_t b(new B());
b->f();
return 0;
}

the program will be terminated with the following message

terminate called after throwing an instance of 'boost::bad_weak_ptr'
what(): boost::bad_weak_ptr
Aborted

-- 
Regards,
R. P. Janaka


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