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);
BOOST_REQUIRE_EQUAL(1, pB.use_count());
{
shared_ptr<A> pA = pB->FooA();
BOOST_REQUIRE_EQUAL(2, pB.use_count());
BOOST_REQUIRE_EQUAL(pB.use_count(), pA.use_count());
}
BOOST_REQUIRE_EQUAL(1, pB.use_count());
}
-------------
// Johan