Johan Nilsson wrote:
Russell Hind wrote:
I thought each time you inherit from enable_shared_from_this<T> you get a new reference count, so with the code below, won't you end up with 2 separate reference counts to the same instance? I thought this was a limitation of shared_from_this.
The following passes for me (RC_1_34_0, vc8):
------- #include
#include using boost::shared_ptr; using boost::enable_shared_from_this;
struct A : enable_shared_from_this<A> {};
struct B : A, enable_shared_from_this<B> { shared_ptr<B> FooB() { return enable_shared_from_this<B>::shared_from_this(); }
shared_ptr<A> FooA() { return enable_shared_from_this<A>::shared_from_this(); } };
BOOST_AUTO_TEST_CASE(EnableSharedFromThisTest) { shared_ptr<B> pB(new B);
I have no idea why this compiles. It shouldn't, B has two enable_shared_from_this<> bases, and the constructor will only initialize one of them. Only one of FooA and FooB works (FooA in this case). I'll investigate.